一共是三道题,第一道题,我已经做出来了,但是因为第一道题和后面两道题是有关联的所以一起发出来了。
Q1
The when it is compressed by an amount x m by the mass, the force, F(x), in the spring is given to be F(x) =k1x^a+k2x^b. Produce a plot showing the variation of F vs. x for 0<=x<=6*h

ANS: 
function [f]=f(x)
u=3;
v=4;
w=2;
x=0;
y=1;
z=1;
k1=45000*(1+u)
k2=8100*(1+v)
m=960*(1.2-0.1*w)
h=0.43*(1+0.1*x)
g=9.8*(1+0.01*y)
a=1+0.01*z
b=1.5+0.01*u
x = 0:0.01:6*h;
f=k1*x.^a+k2*x.^b
plot(x,f)

Q2
Conservation of energy is used and showed below: 
k1/(1+a)*d^(1+a)+k2/(1+b)*d^(1+b)=mgd+mgh
Use the bisection method to determine an approximate value of d, and the absolute error of less than 1*10^-6

我的答案:

function Q2
%solve question 2 with bisection method
%calculate the constant
k1=180000;
k2=40500;
m=960;
h=0.4300;
g=9.8980;
a=1.0100;
b=1.5300;
%specify number of bisection steps
n=1000;
%define and print out the initial interval
A=0;
B=k1*(6*h)^a+k2*(6*h)^b;
fprintf('\n initial interval [%g,%g]\n',A,B)
%initialise and check that there is a root in the prescribed interval
x_left=A;
x_right=B;
f_left=f(x_left);
f_right=f(x_right);
if f_left*f_right>0
error('ERROR:no root in the specified interval')
end
%the bisection method
for i=1:n
%check that the root does not happen to exactly coincide with one of
%the end points
if f_left==0
fprintf('\n stage %g root %g with zero absolute error \n',i,x_left);
return;
end
if f_right==0
fprintf('\n stage %g root %g with zero absolute error \n',i,x_right);
return;
end
%the bisection process
x_mid=(x_left+x_right)/2.0;
f_mid=f(x_mid);
if f_left*f_mid<=0
%there is a root in [f_left,f_mid]
x_right=x_mid;
f_right=f_mid;
end
if f_left*f_mid>0
%there is a root in [x_mid,x_right]
x_left=x_mid;
f_left=f_mid;
end
root=(x_left+x_right)/2.0
abs_erro=(x_right-x_left)/2.0
fprintf('\n stage %g root %g absolute error <%g \n',i,root,abs_erro);
end
%check satisfaction of equation at end of process
residual=f(root);
fprintf('\n final residual = %g\n',residual);
end
%sub-function
function [f_value]=f(x)
f_value=k1/(1+a)*x^(a+1)+k2/(1+b)*x^(b+1)-m*g*x-m*g*h;
end

其中函数取值是第一道题F的取值范围~我不知道该如何将第一道题F的答案作为第二道题的取值范围,还有,我所写,在运行时有出错误,我是按照老师给的过程写的,步骤是没有错的,不过就是不对。

Q3
Use the trapezoidal rule to obtain an approximate value of E, which is energy and is expressed as: E=F(x)的积分,取值是从0到d, with the relative error of less than 0.001%

我的答案:
function [integral]=Q3(m)
a=0;
b=d;
n=m;
h=(b-a)/n
integral=0;
for m=1:n
x_left=a+(m-1)*h;
x_right=a+m*h;
f_left=f(x_left);
f_right=f(x_right);
integral=integral+h/2*(f_left+f_right);
end
fprintf('\n Approximation to integral with %g \n',n,integral);
function [f_value]=f(x)
f_value=180000*x^1.0100+40500*x^1.5300;

这道题也是不知道该怎么把第二题的答案作为这道题的取值范围,而且并没有进行运算,所以并不知道写的对不对,不过也是按着老师的步骤写的。2、3题的步骤都没有问题,请能人帮忙指点一下~谢谢了!!