ode euler - explicit method
조회 수: 15 (최근 30일)
이전 댓글 표시
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
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!