Matlab 打印图形错误(未显示值)

Matlab 打印图形错误(未显示值),matlab,Matlab,当以下代码中的s发生变化时,如何绘制近似值-答案?如果你看我下面的代码,你可以看到我使用的方法(我把它放在一个单独的文件中) 但是,它没有显示从1到1000的图形。相反,图形是从999到1001的,并且没有任何点 for s = 1:1000 error = LaplaceTransform(s,5) - (antiderivative(1,s)-antiderivative(0,s)); end plot(s,error); title('Accuracy of Approximati

当以下代码中的
s
发生变化时,如何绘制
近似值-答案
?如果你看我下面的代码,你可以看到我使用的方法(我把它放在一个单独的文件中)

但是,它没有显示从
1
1000
的图形。相反,图形是从
999
1001
的,并且没有任何点

for s = 1:1000
    error = LaplaceTransform(s,5) - (antiderivative(1,s)-antiderivative(0,s));
end
plot(s,error);
title('Accuracy of Approximation');
xlabel('s');
ylabel('Approximation - Exact Answer');
使用的功能包括:

function g = LaplaceTransform(s,N);
% define function parameters
a=0; 
b=1;
h=(b-a)/N;
x = 0:h:1;
% define function
g = ff(x).*exp(-s*x);

% compute the exact answer of the integral
exact_answer=antiderivative(b,s)-antiderivative(a,s)
% compute the composite trapezoid sum
If=0;
for i=1:(N-1)
    If=If+g(i).*h;
end;
If=If+g(1).*h/2+g(N).*h/2;
If

任何帮助都将不胜感激。谢谢。

以下内容

for s = 1:1000
    error = LaplaceTransform(s,5) - (antiderivative(1,s)-antiderivative(0,s));
end
plot(s,error);
已经有几个问题了。正如@Amro所指出的,
error
在每次迭代中都会被覆盖,而循环变量
s
是一个标量

因此,你需要写作

difference = zeros(1000,1); %# preassignment is good for you
for s = 1:1000
    difference(s) = LaplaceTransform(s,5) - (antiderivative(1,s)-antiderivative(0,s));
end
plot(1:1000,difference);
LaplaceTransform
函数中还有一个错误

function g = LaplaceTransform(s,N);
[...]
g = ff(x).*exp(-s*x); %# g is an array

[...]
If %# If is calculated, but not returned.
我想你是想写信吧

function If = LaplaceTransform(s,N);

相反,因为否则,您将尝试将数组
g
分配给标量
差异

,您将在每次迭代中覆盖
错误
变量。而是将值存储在向量中:
error=…
并将结果打印为
plot(1:1000,error)
。另一方面,ERROR是一个内置函数,因此避免在赋值a(I)=B中使用它作为变量名,B和I中的元素数必须相同。错误==>图在2个差异处==拉普拉斯变换(s,50)-(反导数(1,s)-反导数(0,s));不确定在运行此代码时如何修复该错误:对于s=1:1000差异(s)=LaplaceTransform(s,50)-(反导数(1,s)-反导数(0,s));终点图(1:1000,差值);我想你是……的主人。。以前的一些问题仍然存在:您应该从
LaplaceTransform
函数返回
If
not
g
。函数本身现在工作正常,正在计算正确的值。我现在遇到的问题是作图。我得到这个错误:???在赋值A(I)=B中,B和I中的元素数必须相同。错误==>图在3个差分处=LaplaceTransform(s,5)-(反导数(1,s)-反导数(0,s));我也不想成为函数。g是我想要的函数,当我运行它时,程序运行得非常好。好吧,现在我把函数改为if,它似乎工作得更好了。
function g = LaplaceTransform(s,N);
[...]
g = ff(x).*exp(-s*x); %# g is an array

[...]
If %# If is calculated, but not returned.
function If = LaplaceTransform(s,N);