Problem 4. Simulating dynamical systems in Matlab Read the Wikipedia page about
ID: 2073758 • Letter: P
Question
Problem 4. Simulating dynamical systems in Matlab Read the Wikipedia page about Nu- merical methods for ordinary differential equations (https://en.wikipedia.org/wiki/ Numerical-methods-for-ordinary-differential-equations). Given y(t)=f(t,y(t)) -y(t) + u(t), use the Euler method to simulate the behavior of the ODE for the following control inputs and initial conditions a) Zero input, non-zero initial state: u(t) =0, y(0) = 21 b) Non-zero input, zero initial state: u(t) = cos(t), y(0) = 0 c) Non-zero input, non-zero initial state: u(t) = exp(-1.5t), y(0) =-4 Plot the response of the system versus time for each of the cases in an interval from t [0, 10]. HINT: Make sure to make your time step, AT, small enough catch all of the system behavior If changing your value of AT changes how the system behaves, then it is too big. I recommend trying something around 1millisecond to start with.Explanation / Answer
Matlab Code:
main.m
%Main function to caluculate solution of a differential equation using
%Eulers method that is defined in EulerResult.m
F = @(y,u) u-y; % Anonymous function to calculate output of the differential equation
U = @(t) exp(-1.5*t);% Anonymous function to calculate Input of the differential equation
t0 = 0; % Start time
tfinal = 10; % Stop time
h = 0.001; % step size
y0 = -4; % Initil value of the output
[t,y] = EulerResult(F,U,t0,tfinal,h,y0); % Calculate output
plot(t,y)
EulerResult.m:
% Function to calculate output in next time step using Euler method.
% Outputs
% t = time vector with sampling time steps
% y = solution of the differential equation
% Inputs
% F = f(y,t) Anonymous function to calculate output of the differential equation
% U = u(t). Anonymous function to calculate Input of the differential equation
% t0 = Start time
% tfinal = Stop time
% h = step size
% y0 = Initil value of the output
function [t,y] = EulerResult (F,U,t0,tfinal,h,y0)
t=t0:h:tfinal; % Time vector
y(1)= y0; % Initial y output
for i=1:length(t)-1
u(i) = U(t(i)); % Calclation of input value
y(i+1) = y(i)+h*F(y(i),u(i)); % Calculation of output value
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.