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

 채택된 답변

James Tursa
James Tursa 2020년 4월 27일

0 개 추천

Add this after each figure statement
hold on

댓글 수: 3

Yo mama
Yo mama 2020년 4월 27일
This worked but what happens when I run it is that it will run through each line but then only keep one at the end for both plots and the displacent and velocity graph will be the exact same
Yo mama
Yo mama 2020년 4월 27일
update i had a small syntax error it works now disregard the last post
Denisha Nez
Denisha Nez 2021년 10월 26일
What was the error actually?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

질문:

2020년 4월 27일

댓글:

2021년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by