self_ fun=@(x) (2.*x.*x-3*x+4+sin(x) +exp(x)); deri=@ (x) (4*x-3+cos (x) +exp(x)); deri_ 1=@(f,x,h) ((f(x+h)-f(x)) ./h); x=0:0.1:1; x=x'; y=deri (x); h0=0 .001; y1=deri_ 1 (self_ fun,x,h0); y2=deri_ 2(self_ fun,x,h0); plot (x,abs(y1-y),'-ro',x,abs(y2-y),'--b*'); legend('error of deri 1','error of deri_ 2')5 xlabel('x (Step h is set as 0.001)'); ylabel ('Error of deriviation approximate functions') ; x0=0; d=[(2): (1): (14)]'; h=10.^(-d); yy=deri (x0) *ones (size (d) ); yy1-deri_ 1(self_ fun, x0,h) yy2=deri_ 2(self_ fun,80,hl; figure; plot (d,abs (yy1-yYY/'-ro'); legend('errorof deri. 1'); xlabel('Step h (at the point of x=0)'); ylabel ('Error of deriviation approximate function');此代码在octave运行中报错,能解释一下吗
时间: 2023-08-10 20:05:17 浏览: 444
这段代码中有几个拼写错误:
1. `deri_ 1` 应该改为 `deri_1`
2. `deri_ 2` 应该改为 `deri_2`
3. `yy1-deri_ 1` 中的减号应该改为等号,即 `yy1=deri_1`
除此之外,还有一些语法错误:
1. `legend('error of deri 1','error of deri_ 2')5` 中,逗号应该改为英文逗号 `,`,即 `legend('error of deri 1','error of deri_ 2')`
2. 在 `yy1=deri_1(self_fun, x0,h)` 和 `yy2=deri_2(self_fun,80,hl)` 中,函数名 `self_fun` 应该改为 `self_fun(x)`
3. 在 `plot (d,abs (yy1-yYY/'-ro')` 中,应该将引号 `'` 放在右括号 `)` 的前面,即 `plot (d,abs (yy1-yy),'-ro')`
修改后的代码如下:
```
self_fun = @(x) (2.*x.*x-3*x+4+sin(x) +exp(x));
deri = @(x) (4*x-3+cos(x) +exp(x));
deri_1 = @(f,x,h) ((f(x+h)-f(x)) ./h);
x = 0:0.1:1;
x = x';
y = deri(x);
h0 = 0.001;
y1 = deri_1(self_fun,x,h0);
y2 = deri_2(self_fun,x,h0);
plot(x,abs(y1-y),'-ro',x,abs(y2-y),'--b*');
legend('error of deri 1','error of deri_2')
xlabel('x (Step h is set as 0.001)');
ylabel('Error of deriviation approximate functions');
x0 = 0;
d = [(2):(1):(14)]';
h = 10.^(-d);
yy = deri(x0)*ones(size(d));
yy1 = deri_1(self_fun,x0,h);
yy2 = deri_2(self_fun,x0,h);
figure;
plot(d,abs(yy1-yy),'-ro');
legend('error of deri. 1');
xlabel('Step h (at the point of x=0)');
ylabel('Error of deriviation approximate function');
```
阅读全文