1 view (last 30 days)

Hi everyone,

I am trying to use Euler's method to solve y'= 11/(100*(t+1)^0.9) between t=1 and t=25. My script gives an answer that seems to be close but not exactly right. It shows an answer of 5.17 and the expected answer is 5.3ish. I've been going over this for hours and can't see where it goes wrong or if it is just due to rounding? Any advice would be appreciated.

Also, If someone could explain or hint at how to graph the velocity and position in the same figure? They are different sizes which matlab doesn't like and everything I've tried thus far has failed.

Thanks for all the help you can provide! I appreciate it a lot!

clear all;

close all;

%Set the 3 initial conditions below

tmin=1; %Inital value of t. Can be changed if needed

tmax=25; %Final value of t. Can be changed if needed

N=10000; %number of steps that you wish to use. Can also be changed

x=zeros(1,N); %allocates memory for matrix of size 1xN

dt=(tmax-tmin)/N; %defines the step size

x(1)=11/10; %initial value of the function at time t=0

t=tmin:dt:tmax; %t is the array of values from tmin to tmax increasing by the step size

for i=1:N;

dxdt(i)= 11/(100*(i*(dt)+1)^0.9);%determines the value of the derivative at each step

x(i+1)=x(i)+dt*dxdt(i);%uses the value of the derivative at a certain position and the position at that step to determine the new position

end; %ends the for loop

figure(1); %creates figure 1

plot(t,x); %Plots each step value from tmin to tmax vs the determined position at that point

xlabel ('Time (s)'); %labels x axis as time in seconds

ylabel ('Position (m)'); %labels y axis as position in meters

grid on; %turns the grid to the function on to make it easier to read

hold on; %keeps this plot on the graph and allows the next plot to be put on the same graph

xlim ([tmin tmax]); %makes the x axis only show time from tmin to tmax

Aquatris
on 7 Sep 2018

Edited: Aquatris
on 7 Sep 2018

The reason is simple. The time in your code start from 1 where as online calculators start from 0.

If you adjusted your equation to account for the start time (t+1 term), then you need to input t = 24 in the online calculator solution.

The online calculator solution is;

x = 1.1*(t+1)^(1/10);

If you put t = 24, which means 25 seconds in reality due to +1 adjustment (online calculator answers t range is 0 to 24, with the +1 adjustment you make it 1 to 25), you will get;

x = 1.1*(t+1)^(1/10)

x = 1.1*(24+1)^(1/10)

x = 1.5177

which is what your Euler method finds.

Walter Roberson
on 7 Sep 2018

I suspect it should be more like

[T,Y] = ode45(@(T,t) 11/(100*(t+1)^0.9), [0 25],0);

mask = T >=1;

plot(T(mask), Y(mask,:))

Y(end)

ans =

1.61799056393637

That is, the problem reads to me as if time starts at 0 but that plotting is to be done from time 1 to 25.

Aquatris
on 7 Sep 2018

I agree there is some ambiguity here. I think it all depends on what the actual problem states.

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/417942-eulers-method-returning-an-answer-close-but-not-exactly-correct#comment_607566

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/417942-eulers-method-returning-an-answer-close-but-not-exactly-correct#comment_607566

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/417942-eulers-method-returning-an-answer-close-but-not-exactly-correct#comment_607568

⋮## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/417942-eulers-method-returning-an-answer-close-but-not-exactly-correct#comment_607568

Sign in to comment.