odeEueler Explicit Eurlers method

2 views (last 30 days)
Ok so I am working on a project and I will put the image of the problem statement and there must be somthing fundemental that I am doing incorrectly because I feel like this methodology is right for the problem
Here is my host file
%% Definining Initial Parameters for the Eueler
%yINI = [0;0]; %This describes the IC for the displacement and velocity
%h = [0.8 0.5 0.1]; %Given step sizes we will use in plots
%te = [0 8]; % ' Time Elapsed' from 0s-->8s our x-axis
function [t,y,ydot] = odeEULER(ODE1,ODE1,a,b,h,yINI)
%a - initial value for t
%b - last value of t
%h - step size
%yINI - y and y dot initial values
%Output variables- t,y,ydot
t(1) = 0; y(1) = yINI; ydot = yINI;
N= (b-a)/h;
for i=1:N
t(i+1)=t(i) + h;
y(i+1)=y(i) + ODE1(t(i),y(i))*h;
ydot(i+1)=ydot(i) + ODE2(t(i),ydot(i))*h;
end
and now here is my scrpit file:
clc; clear all
a=0;b=8;h = [0.8 0.5 0.1];yINI = [0;0];
[t,y,ydot] = odeEULER(@dydt,@dydotdt,a,b,h,yINI)
figure(1)
plot(t,y,'LineWidth',2)
xlabel('Time(s)')
ylabel('Distance travelled(m)')
title('Displacement over time')
grid on
figure(2)
plot(t,ydot,'LineWidth',2)
xlabel('Time(s)')
ylabel('Velocity (m/s)')
title('Velocity vs Time')
grid on
function dydx=dydt(t,y,ydot)
dydx=ydot;
end
function dydx=dydotdt(t,y,ydot)
g=32.2,w= 3000-800*t;T = 8000;D =((0.005*g)*(ydot^2));
dydx=((g/w)*(T-w-D));
end
Please let me know what I am to

Accepted Answer

Thiago Henrique Gomes Lobato
You had many syntax errors on your code. Here is a version with them fixed and it should work. Although a negative weighting is not something that makes sense to me (it happens for t>3.75 s following your command).
clc; clear all
a=0;b=8;h = [0.8 0.5 0.1];yINI = [0;0];
for idxH=1:3
[t,y,ydot] = odeEULER(@dydt,@dydotdt,a,b,h(idxH),yINI);
figure
plot(t,y,'LineWidth',2)
xlabel('Time(s)')
ylabel('Distance travelled(m)')
title(['Displacement over time. h = ',num2str(h(idxH))])
grid on
figure
plot(t,ydot,'LineWidth',2)
xlabel('Time(s)')
ylabel('Velocity (m/s)')
title(['Velocity vs Time. h = ',num2str(h(idxH))])
grid on
end
function dydx=dydt(ydot)
dydx=ydot;
end
function dydx=dydotdt(t,ydot)
g=32.2;
w= 3000-800*t;T = 8000;D =((0.005*g)*(ydot^2));
dydx=((g/w)*(T-w-D));
end
function [t,y,ydot] = odeEULER(ODE1,ODE2,a,b,h,yINI)
%a - initial value for t
%b - last value of t
%h - step size
%yINI - y and y dot initial values
%Output variables- t,y,ydot
N= (b-a)/h;
y = zeros(N,1);
ydot = zeros(N,1);
t = zeros(N,1);
t(1) = 0; y(1) = yINI(1); ydot(1) = yINI(2);
for i=1:N-1
t(i+1)=t(i) + h;
y(i+1)=y(i) + ODE1(ydot(i))*h;
ydot(i+1)=ydot(i) + ODE2(t(i),ydot(i))*h;
end
end
  1 Comment
Yo mama
Yo mama on 26 Apr 2020
Edited: Yo mama on 27 Apr 2020
Thank you this is helpful i see where I went wrong but the only thing Ive been trying to problem solve is how would I get the 3 different h values lines on one plot would you say. This outputs 6 graphs (3 for displacement and 3 for velocity for each 3 step sizes) How would I put those three lines in the same plot for each

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!