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에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!