Displaying % error
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
Fangjun Jiang
am 23 Nov. 2011
t=1:10;
y=1:10;
str=cellstr(num2str(y.','%%%5.2f'));
plot(t,y);
text(t,y,str);
3 Kommentare
Fangjun Jiang
am 23 Nov. 2011
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Number Theory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!