Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
euler's method

Subject: euler's method

From: j mac

Date: 6 Apr, 2011 17:09:41

Message: 1 of 6

hi im getting an error that i dont understand i havent used matlab
much but here is my code


% The problem to be solved is:

%y'=y*(2-y)


%exact solution
% y(t)=6*exp(2*x)/(3*exp(2*x)-1)

h=.1; %h=time step
x=0:h:.5; %time variable

clear ystar;

ystar(1)=3; %initial condition

for i=1:length(x)-1, %Estimate loop
   k1=(2*ystar(i)-ystar(i).^2);
   ystar(i+1)=ystar(i)+h*k1; %Estimate new y
end

%exact solution
y=6*exp(2*x)/(3*exp(2*x)-1)

%Print results
for i=1:length(x)
   disp(sprintf('x=%5.3f, y(x)=%6.4f, y*(x)=
%6.4f',x(i),y(i),ystar(i)));
end

%Plot approximate and exact
plot(x,ystar,'b--',x,y,'r-');
legend('Approximate','Exact');
title('Euler Approximation');
xlabel('Time');
ylabel('y*(x), y(x)');

the error that i am getting is
??? Attempted to access y(2); index out of bounds because
numel(y)=1.

Error in ==> my_euler at 27
   disp(sprintf('x=%5.3f, y(x)=%6.4f,
   y*(x)=%6.4f',x(i),y(i),ystar(i)));

does anyone know why its giving this error and or how to fix it

Subject: euler's method

From: Florin Neacsu

Date: 6 Apr, 2011 17:44:04

Message: 2 of 6

j mac <jwm5506@gmail.com> wrote in message <49e9cc23-28e4-4cdc-b6f6-2833e1103c24@w36g2000vbi.googlegroups.com>...
> hi im getting an error that i dont understand i havent used matlab
> much but here is my code
>
>
> % The problem to be solved is:
>
> %y'=y*(2-y)
>
>
> %exact solution
> % y(t)=6*exp(2*x)/(3*exp(2*x)-1)
>
> h=.1; %h=time step
> x=0:h:.5; %time variable
>
> clear ystar;
>
> ystar(1)=3; %initial condition
>
> for i=1:length(x)-1, %Estimate loop
> k1=(2*ystar(i)-ystar(i).^2);
> ystar(i+1)=ystar(i)+h*k1; %Estimate new y
> end
>
> %exact solution
> y=6*exp(2*x)/(3*exp(2*x)-1)
>
> %Print results
> for i=1:length(x)
> disp(sprintf('x=%5.3f, y(x)=%6.4f, y*(x)=
> %6.4f',x(i),y(i),ystar(i)));
> end
>
> %Plot approximate and exact
> plot(x,ystar,'b--',x,y,'r-');
> legend('Approximate','Exact');
> title('Euler Approximation');
> xlabel('Time');
> ylabel('y*(x), y(x)');
>
> the error that i am getting is
> ??? Attempted to access y(2); index out of bounds because
> numel(y)=1.
>
> Error in ==> my_euler at 27
> disp(sprintf('x=%5.3f, y(x)=%6.4f,
> y*(x)=%6.4f',x(i),y(i),ystar(i)));
>
> does anyone know why its giving this error and or how to fix it


Hi,

Try
y=6.*exp(2.*x)./(3.*exp(2.*x)-1);
instead of
y=6*exp(2*x)/(3*exp(2*x)-1)

Regards,
Florin

Subject: euler's method

From: Nasser M. Abbasi

Date: 6 Apr, 2011 18:23:59

Message: 3 of 6

On 4/6/2011 10:44 AM, Florin Neacsu wrote:

>
> Hi,
>
> Try
> y=6.*exp(2.*x)./(3.*exp(2.*x)-1);
> instead of
> y=6*exp(2*x)/(3*exp(2*x)-1)

No need to have a '.' when multiplying a scalar by a vector.

so

   "y=6.*exp(2.*x)./(3.*exp(2.*x)-1);"

can be just


   y=6*exp(2*x)./(3*exp(2*x)-1);

adding '.' when it is not needed makes the code harder to read.

--Nasser

Subject: euler's method

From: Florin Neacsu

Date: 6 Apr, 2011 18:37:04

Message: 4 of 6

"Nasser M. Abbasi" <nma@12000.org> wrote in message <inib3t$ck8$1@speranza.aioe.org>...
> On 4/6/2011 10:44 AM, Florin Neacsu wrote:
>
> >
> > Hi,
> >
> > Try
> > y=6.*exp(2.*x)./(3.*exp(2.*x)-1);
> > instead of
> > y=6*exp(2*x)/(3*exp(2*x)-1)
>
> No need to have a '.' when multiplying a scalar by a vector.
>
> so
>
> "y=6.*exp(2.*x)./(3.*exp(2.*x)-1);"
>
> can be just
>
>
> y=6*exp(2*x)./(3*exp(2*x)-1);
>
> adding '.' when it is not needed makes the code harder to read.
>
> --Nasser
>

Hi,
Indeed there is no need for those ones.
Regards,
Florin

Subject: euler's method

From: Florin Neacsu

Date: 6 Apr, 2011 18:46:04

Message: 5 of 6

j mac <jwm5506@gmail.com> wrote in message <49e9cc23-28e4-4cdc-b6f6-2833e1103c24@w36g2000vbi.googlegroups.com>...
> hi im getting an error that i dont understand i havent used matlab
> much but here is my code
>
>
> % The problem to be solved is:
>
> %y'=y*(2-y)
>
>
> %exact solution
> % y(t)=6*exp(2*x)/(3*exp(2*x)-1)
>
> h=.1; %h=time step
> x=0:h:.5; %time variable
>
> clear ystar;
>
> ystar(1)=3; %initial condition
>
> for i=1:length(x)-1, %Estimate loop
> k1=(2*ystar(i)-ystar(i).^2);
> ystar(i+1)=ystar(i)+h*k1; %Estimate new y
> end
>
> %exact solution
> y=6*exp(2*x)/(3*exp(2*x)-1)
>
> %Print results
> for i=1:length(x)
> disp(sprintf('x=%5.3f, y(x)=%6.4f, y*(x)=
> %6.4f',x(i),y(i),ystar(i)));
> end
>
> %Plot approximate and exact
> plot(x,ystar,'b--',x,y,'r-');
> legend('Approximate','Exact');
> title('Euler Approximation');
> xlabel('Time');
> ylabel('y*(x), y(x)');
>
> the error that i am getting is
> ??? Attempted to access y(2); index out of bounds because
> numel(y)=1.
>
> Error in ==> my_euler at 27
> disp(sprintf('x=%5.3f, y(x)=%6.4f,
> y*(x)=%6.4f',x(i),y(i),ystar(i)));
>
> does anyone know why its giving this error and or how to fix it

Hi again,

I know this is off the topic but you should be careful with ystar (if you noticed in the m editor, ystar might be underlined in orange). That is matlab telling you, that ystar is increasing inside a for loop. In your example is not an issue (only 5 iterations), but as soon as the vector becomes bigger it will be time consuming. Consider preallocating (y=zeros(size(x)));
Regards,
Florin

Subject: euler's method

From: j mac

Date: 6 Apr, 2011 19:36:58

Message: 6 of 6

thanks so much for the help i will give this a try

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us