Plotting Error
5 次查看(过去 30 天)
显示 更早的评论
The following is my code, everything works how I want it to but I need to plot the error on another plot, how do I do this?
%%Analytical
simplify(dsolve('Dy=-x/y','y(0)=5','x'));
%%Numerical
f=@(x) (-x^2+25)^(1/2);
dydx=@(x,y) -(x/y);
[x1,y1]=eulode(dydx, [0 5],5,.5);
[x2,y2]=eulode(dydx,[0 5],5,.1);
[x3,y3]=eulode(dydx,[0 5],5,.01);
disp([x1,y1])
disp([x2,y2])
disp([x3,y3])
%%Percent Error
x1=0:.5:5;
x2=0:.1:5;
x3=0:.01:5;
analytical_step1= (-x1.^2+25).^(1/2);
analytical_step2=(-x2.^2+25).^(1/2);
analytical_step3=(-x3.^2+25).^(1/2);
numerical_1=[y1]';
numerical_2=[y2]';
numerical_3=[y3]';
Percent_Error1=abs((analytical_step1-numerical_1)/analytical_step1)*100%answer displayed in percent
Percent_Error2=abs((analytical_step2-numerical_2)/analytical_step2)*100%answer displayed in percent
Percent_Error3=abs((analytical_step3-numerical_3)/analytical_step3)*100%answer displayed in percent
%%Plot
plot(x1,y1,'k',x2,y2,'b',x3,y3,'g','linewidth',2)
hold on
fplot(f,[0 5],'Linewidth',2,'r')
legend('STEP 0.5','STEP 0.1','STEP 0.01','Analytical','Location','W')
grid
title('Plot of Analytical Function and Numerical Approximations')
0 个评论
回答(3 个)
John
2011-3-28
3 个评论
Matt Tearle
2011-3-28
Ahhhh.... just noticed something: you're using / instead of ./ in the calculation of the error. That returns a scalar from the vectors analytical_step and numerical, but it's probably not what you want.
John
2011-3-29
3 个评论
Matt Tearle
2011-3-30
In that case, yes, your code is working as you want, as far as that goes. Some minor things: "title", not "Title"; you're missing a 'location' keyword in your legend command; and you might want to consider using semilogy rather than plot.
The bigger issue is still your calculation of Percent_ErrorX. Are you sure this is doing what you want? In MATLAB, x = A/B is interpreted as "solve the system of equations x*B = A (for x)". Your A & B are the row vectors analytical_step1 and (analytical_step1-numerical_1). xB = A is equivalent to B'x = A'. This is the standard setup for a regression, with A' as the response variable and B' as the matrix of predictors. Hence, you're essentially doing a regression of the form y = kx where x is the analytic solution, and y is the error. However, if you plot these vectors against each other, you'll see that they are not related by that equation. (You could possibly get a linear regression, but you'd need a constant term.)
Bottom line: I don't think (true-numeric)/true is what you want. Generally, numerical error is reduced to a single value by taking some simple vector-norm aggregate. ie Percent_Error1 = norm(PE1,1)
or norm(PE1,2) or norm(PE1,Inf).
But....! This will cause problems because your last error is Inf, due to the analytic solution being 0. So you'll need to figure out how you want to deal with that. norm(PE1(isfinite(PE1)),Inf) would be one way.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!