Eulers Method Returning an Answer close but not exactly correct
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2018-9-7
编辑:Walter Roberson
2018-9-7
I am not clear what it means to "solve" it? Are you trying to solve
int(11/(100*(t+1)^0.9), t, 1, x) == 0
with the constraint that x <= 25?
Perhaps you are trying the equivalent of
[T,Y] = ode45(@(T,t) 11/(100*(t+1)^0.9), [1 25],0);
Y(end)
That gives about 1.57135483024211
回答(1 个)
Aquatris
2018-9-7
编辑: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 个评论
Walter Roberson
2018-9-7
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
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!