# 数值分析-matlab 第一章

1.1  例题解答

>> syms x;
>>f=sym('sin(x)')
f =
sin(x)

>> h=taylor(f,8,0)
h =
x-1/6*x^3+1/120*x^5-1/5040*x^7

>> subs(h,x,0.5)
ans =
0.479425533234127

>>H=inline(h)
H =
Inline function:
H(x) = x-1./6.*x.^3+1./120.*x.^5-1./5040.*x.^7
>>feval(H,0.5)
ans =
0.479425533234127

>> I=int('1/(1+x)','x',0,1)
I =
log(2)

>>x=0:0.2:1;  %将[0,1]等分为4等份
>>f=1./(1+x); %分别计算每一个等分点的函数值
>>I=0;
>>for i=1:5
I=I+(f(i)+f(i+1))/2*0.2;  %将每一小曲边的梯形累加起来作为积分值
End
>> vpa(I,9)    %取结果的小数精度为9位小数
ans =
.695634921

>> A=sym('a');
>> sqrt(A)
ans =
a^(1/2)

function x=msqrt(x0,a)
%用迭代法近似计算平方根
%x0为初始迭代值,a为开平方数

format long;
x=zeros(20,1);
x(1)=x0;
for i=2:20
x(i)=1/2*(x(i-1)+a/x(i-1));
end
disp(x);

>> msqrt(2,3);
2.000000000000000
1.750000000000000
1.732142857142857
1.732050810014727
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877
1.732050807568877

>> 1/759-1/760
ans =
1.7336e-006

>> a=1/759,b=1/760,a-b
a =
0.0013
b =
0.0013
ans =
1.7336e-006

>> a=sym('1/759'),b=sym('1/760'),c=a-b
a =
1/759
b =
1/760
c =
1/576840

>> x=solve('x^2-18*x+1','x')
x =
9+4*5^(1/2)
9-4*5^(1/2)

>> vpa(x,6)
ans =

17.9443
.5572e-1

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120