matlab - Plotting graph error (values not showign up) -
how plot value of approximation - answer
s
varies in code below? if @ code below, can see method used (i put in separate file).
however, not show me graph 1
1000
. instead graph 999
1001
, not have points on it.
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');
the functions used:
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 exact answer of integral exact_answer=antiderivative(b,s)-antiderivative(a,s) % compute composite trapezoid sum if=0; i=1:(n-1) if=if+g(i).*h; end; if=if+g(1).*h/2+g(n).*h/2; if
with
function fx=ff(x) fx=x;
and
function fx=antiderivative(x,s); fx= (-exp(-s*x)*(s*x+1))/(s^2);
any appreciated. thanks.
the following
for s = 1:1000 error = laplacetransform(s,5) - (antiderivative(1,s)-antiderivative(0,s)); end plot(s,error);
already has several issues. 2 main ones error
getting overwritten @ each iteration, @amro has pointed out, , s
, loop variable, scalar.
thus, need write
difference = zeros(1000,1); %# preassignment s = 1:1000 difference(s) = laplacetransform(s,5) - (antiderivative(1,s)-antiderivative(0,s)); end plot(1:1000,difference);
there error in laplacetransform
function
function g = laplacetransform(s,n); [...] g = ff(x).*exp(-s*x); %# g array [...] if %# if calculated, not returned.
i assume want write
function if = laplacetransform(s,n);
instead, because otherwise, try assign array g
scalar difference(s)
.
Comments
Post a Comment