フィルターのクリア

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 Center および File ExchangeNumber Theory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by