ode euler - explicit method
13 views (last 30 days)
Show older comments
Basically this is so close to being right but what I need is to plot 3 different lines on each plot for the h values and right now this is only plotting 1. If i tweak it a little bit it will show the legend with the 3 lines but the plot itself wont have it. pls help
%% Defining the Initial Parameters Given in Problem Statement
a=0;b=8; %Time constraints
h=[0.8 0.5 0.1]; %Step sizes that are needed to plot function
yINI= [0;0]; %Initial Conditions
%% Script File for Explicit Eueler - MATLAB Program 10-1
for ih=1:3 %Stringing the step size into the plot
[t,y,ydot] = odeEULER(@dydt,@dydotdt,a,b,h(ih),yINI);
figure(1)
plot(t,y(:,1),'LineWidth',2)
xlabel('Time(s)')
ylabel('Distance travelled(m)')
title('Distance Travelled over Time')
lgnd{ih}=sprintf('Step size=%2.2f',h(ih));
legend(lgnd); box on
grid on
figure(2)
plot(t,ydot(:,1),'LineWidth',2)
xlabel('Time(s)')
ylabel('Velocity (m/s)')
title('Velocity vs Time')
grid on
lgnd{ih}=sprintf('Step size=%2.2f',h(ih));
legend(lgnd); box on
end
%% Defining the two seperate First Order ODE's
function dydx=dydt(ydot) %1st First Order ODE defined as the following
dydx=ydot;
end
function dydx=dydotdt(t,ydot) %2nd First Order ODE defined as the following
g=32.2; % Given Values:
w= 3000-800*t;
T = 8000;
D =((0.005*g)*(ydot^2));
dydx=((g/w)*(T-w-D));
end
%% Host file for the Explicit Euler - MATLAB Program 10-1 page 393
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
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!