필터 지우기
필터 지우기

Displaying % error

조회 수: 3 (최근 30일)
Karen
Karen 2011년 11월 23일
can you just provide me another simpler code? based on the codes above, we want to compute the percent error for each h at value at time t =1,2,3,4,5?
Here is my code so far:
%Script that demonstrates Euler integration for a first order problem using
%MATLAB.
%The problem to be solved is:
%y'(t)+2*y(t)=2-exp(-4*t)
%Note: This problem has a known exact solution
%y(t)=1+0.58*exp(-4*t)-0.5*exp(-2*t)
function ystar = Eulermethod20(n)
a=0;
b=5;
h=(b-a)/n;
t=0:h:5;%Initialize time variable
clear ystar;%wipe out old variable
ystar(1)=1.0;%Initial condition (same for approximation)
for i=1:length(t)-1, %Set up "for" loop
k1=2-exp(-4*t(i))-2*ystar(i); %Calculate the derivative
ystar(i+1)=ystar(i)+h*k1;%Estimate new value of y
end
%Exact solution
y=1+0.5*exp(-4*t)-0.5*exp(-2*t);
%Error calculation
percent_error=100*abs(y-ystar)./y;
%Plot approximate and exact solutions
plot(t,ystar,'b--',t,y,'r-',t,percent_error,'g');
legend('Approximate','Exact','Error');
title('Euler Approximation n=5000');
xlabel('Time');
ylabel('y*(t), y(t)');
thanks.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 11월 23일
t=1:10;
y=1:10;
str=cellstr(num2str(y.','%%%5.2f'));
plot(t,y);
text(t,y,str);
  댓글 수: 3
Karen
Karen 2011년 11월 23일
can you just provide me another simpler code? based on the codes above, we want to compute the percent error for each h at value at time t =1,2,3,4,5?
Fangjun Jiang
Fangjun Jiang 2011년 11월 23일
h is your step size. It changes as you change the number of points, n. You already had your code. Just call your function with different n.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by