필터 지우기
필터 지우기

Plotting Error

조회 수: 5 (최근 30일)
John
John 2011년 3월 28일
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')

답변 (3개)

Matt Tearle
Matt Tearle 2011년 3월 28일
Use figure to create another plot window

John
John 2011년 3월 28일
I have tried using figure(2)
however, I do not know which array to call my error and how to plot it I am a little confused.
  댓글 수: 3
Matt Tearle
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
John 2011년 3월 29일
I am wanting to plot the error as a function of the step size, in other words a plot of the error at each point as the function evolves. I think I can just put a ./ in my percent error calculation and plot those values correct?

댓글을 달려면 로그인하십시오.


John
John 2011년 3월 29일
my new code looks like this (I needed a scaler value for error so I left it in my code.
%%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
PE1=abs((analytical_step1-numerical_1)./analytical_step1)*100;
Percent_Error2=abs((analytical_step2-numerical_2)/analytical_step2)*100%answer displayed in percent
PE2=abs((analytical_step2-numerical_2)./analytical_step2)*100;
Percent_Error3=abs((analytical_step3-numerical_3)/analytical_step3)*100%answer displayed in percent
PE3=abs((analytical_step3-numerical_3)./analytical_step3)*100;
%%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
figure
plot(x1,PE1,x2,PE2,x3,PE3,'linewidth',2)
grid
legend('STEP 0.5','STEP 0.1','STEP 0.01','NW')
Title('Percent Error')
ylabel('Percent')
  댓글 수: 3
John
John 2011년 3월 30일
No I am not wanting the scaler values of the percent error I want to plot it as the function progresses, is the correct the way I have it coded?
Matt Tearle
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 CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by