Azzera filtri
Azzera filtri

Initial Value of y(0)=0

6 visualizzazioni (ultimi 30 giorni)
Karen
Karen il 23 Nov 2011
How does one write a code for the IVP of y(0) = 0? The program won't accept the following. Also, can someone check my syntax for the equations I've entered? Thanks!
function ystar = Eulermethod201(n)
a=0;
b=5;
h=(b-a)/n;
t=0:h:5;%Initialize time variable
clear ystar;%wipe out old variable
ystar(0)=0;%Initial condition (same for approximation)
for i=0:length(t), %Set up "for" loop
%Calculate the derivative
k1=(-0.5*exp(t(i)/2)*sin(5*t(i))+5*exp(t(i)/2)*cos(5*t(i))-ystar(i));
ystar(i+1)=ystar(i)+h*k1;%Estimate new value of y
end
%Exact solution
y=(exp(t/2))*(sin(5*t));
%Error calculation
percent_error=100*abs(y-ystar)./y;
disp(percent_error);
%Plot approximate and exact solutions
plot(t,ystar,'b--',t,y,'r-',t,percent_error,'g');
legend('Approximate','Exact','Error');
title('Euler Approximation n=50');
xlabel('Time');
ylabel('y*(t), y(t)');

Risposta accettata

Fangjun Jiang
Fangjun Jiang il 23 Nov 2011
Unlike C language, MATLAB uses 1-based index for vector and matrix. So if you define a 10x1 vector, a=rand(10,1), you refer it as a(1), a(2) till a(10).
If you have the need to indicate some value at time==0, you will need to use some kind of offset to deal with it.
  2 Commenti
Karen
Karen il 24 Nov 2011
If y(0)=0 is an initial value, then what type of offset would work in that situation? Does that mean that I can use y(1) = 0? Or subtract one as in i=1:length(t)-1; and also use y(1)=0? I've tried using y(.000001)=0, but I get the same error message as if I used y(0)=0.
Fangjun Jiang
Fangjun Jiang il 24 Nov 2011
Yes. You can't use y(0)=0 in MATLAB. You will need to do something like this for example
t=0:9;
y=10:10:100;
For any index number i, y(i) always corresponds to t(i), where i is a number from 1 to 10.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by