# Eulers Method Returning an Answer close but not exactly correct

2 ビュー (過去 30 日間)
Pete 2018 年 9 月 7 日
コメント済み: Aquatris 2018 年 9 月 7 日
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
##### 2 件のコメント表示非表示 1 件の古いコメント
Pete 2018 年 9 月 7 日
Sorry that was bad phrasing. I am trying to use Euler's method to approximate what the function will look like using its derivative and its initial value between 1 and 25 seconds. I look at the value at 25 seconds to determine if the script is working correctly.
My issue is that at 25 seconds, the value is close but not quite what online calculators say the answer should be(I'm using them to compare if my code has functioned correctly). Also, I'm unable to graph the velocity (dxdt) and position(x) on the same graph because they are different lengths which I can't seem to find a workaround.

サインインしてコメントする。

### 回答 (1 件)

Aquatris 2018 年 9 月 7 日

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.
##### 2 件のコメント表示非表示 1 件の古いコメント
Aquatris 2018 年 9 月 7 日
I agree there is some ambiguity here. I think it all depends on what the actual problem states.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by