吉林大学 计算方法实验课 实验0.3
代码用octave语言编写。
实验题目:
实验代码及现象:
(1)选择函数,f(x) = sin(x);
(2)同样的步长,比较两种方法。
b = [1,2,3,4,5,6,7,8];%导数1
c = [1,2,3,4,5,6,7,8];%导数2
%f(x)=sin(x)
jd = 10^-5;
for n = 1:8
b(n) = (sin(n+jd)-sin(n))/jd;
end;
for n = 1:8
c(n) = (sin(n+jd)-sin(n-jd))/(2*jd);
end;
disp(b);
disp(c);
(3)同样的方法,比较不同步长。
jd=[1,2,3,4,5,6,7,8,9,10,11,12,13,14];
a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14];
b=[1,2,3,4,5,6,7,8,9,10,11,12,13,14];
%f(x)=sin(x)
%取单点值=5
for n = 1:14
jd(n) = 10^(-n);
end
%disp(jd);已获得精度值
%选择方法1
for n = 1:14
a(n) = (sin(5+jd(n))-sin(5))/(jd(n))-cos(5);
b(n) = (sin(5+jd(n))-sin(5-jd(n)))/(2*jd(n))-cos(5);
end;
disp(a);
plot(a);
hold on;
plot(b);