Why is my plot printing dots instead of a curve???
5 次查看(过去 30 天)
显示 更早的评论
My code is supposed to print a nice curve instead it is printing dots. I have not specified any dots in my loglog line command and I have not set any default to be dots.
sprintf(' x numerical actual error')
for i =1:numsteps
x = a:h:b;
f = -h^2 * rfun(x);
f(1) = 2*h*lambda;
f(end) = 2*h*rho;
p = -1 -0.5*h.* pfun(x(2:end));
P = -1 + 0.5*h.*pfun(x(1:end-1));
q = 2 + h^2* qfun(x);
A = gallery('tridiag',p,q,P);
A(1,1) =-3;A(1,2)=4; A(1,3) =-1;
A(end,end)= 1; A(end,end-1) = -4; A(end,end-2) = 3;
y = A\f';
hold all
actual = exp(x') - (1/3)*x'.^2.*exp(x');
error =abs(y - actual);
% loglog(h,error);
set(gca, 'XScale', 'log', 'YScale', 'log');
while i ==1
n = 1/h;
disp(sprintf('h = %.4f',h'))
for j =3:2:n
fprintf('%3.6f %3.6f %3.6f %3.6f\n',x(j),y(j),actual(j),error(j))
end
loglog(h,error,'k-');
set(gca, 'XScale', 'log', 'YScale', 'log');
break
end
while i ==2
n =1/h;
disp(sprintf('h = %.4f',h))
for j = 5:4:n-2
fprintf('%3.6f %3.6f %3.6f %3.6f\n',x(j),y(j),actual(j),error(j))
end
loglog(h,error,'m-')
break
end
while i ==3
n =1/h;
disp(sprintf('h = %.4f',h))
for j = 9:8:n-2
fprintf('%3.6f %3.6f %3.6f %3.6f\n',x(j),y(j),actual(j),error(j))
end
loglog(h,error,'g-')
break
end
while i ==4
n =1/h;
disp(sprintf('h = %.4f',h))
for j = 17:16:n-2
fprintf('%3.6f %3.6f %3.6f %3.6f\n',x(j),y(j),actual(j),error(j))
end
loglog(h,error,'b-')
break
end
h=h/2;
end
0 个评论
回答(3 个)
Walter Roberson
2014-4-20
Your "h" must be a scalar or else you would have received error messages or warning messages that you did not mention.
You have calls of the form loglog(h,error,'b-') where the color changes, and there is evidence that error is a vector. With h being a scalar, all of those error values are going to be drawn at the same x position. That is going to produce anywhere from a dot to a vertical line -- with the vertical line probably retracing itself several times (the error values are not sorted.) If any of the error values are negative or 0, they will not appear on a loglog() plot.
Are you sure the above is what you desire??
0 个评论
Star Strider
2014-4-20
I did not run your code, but I believe your problem is due to your plotting your ‘error’ variable in your loop.
You only calculate error once:
error =abs(y - actual);
and you don’t assign it as an array.
I suggest you change it to:
error(i) =abs(y - actual);
and then plot it once at the end of your ‘i’ loop. (That means taking the loglog statements out of your loop.)
0 个评论
Image Analyst
2014-4-20
DO NOT USE error AS THE NAME OF A VARIABLE! It is a built in function and you just wiped it out by redefining it as one of your variables. Call it "theError" or "residuals" or something else, but never "error".
Also, we can't run your code because you didn't give all the information, like values for numsteps, a, h, b, rfun, etc.
1 个评论
Star Strider
2014-4-20
Good point! I was also going to caution against ‘i’ and ‘j’ as loop indices, but decided to wait.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!