Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have never used MATLB for anything this complicated, please include some notes with the code. ential equation (t)=f[x(t)] Ean (1) We want to compare
I have never used MATLB for anything this complicated, please include some notes with the code.
ential equation (t)=f[x(t)] Ean (1) We want to compare between the solution of a differential equation And it's equivalent difference equation x(k+1) = x(k) + T f [x(k)] Eqn (2) a) Forf [x(t)]=-x(t) +cos(t) write code to obtain the solution of the differential equation given by equation (1) and the difference equation given by equation (2). You will need to create Matlab code for the iterative solution to the difference equation. Paste your code in the entry block below. b) Compare between the two solutions for three different values of the sampling time. State (in the entry block below) what happens to the difference solution as the sample time varies? Hint: To solve a differential equation, you can use ode45, which is a built in MATLAB function. An example is provided below (for a different function. There are two files. The first contains the function we want to solve and the second file calls the first file during run time. Feel free to pick the initial conditions. An example is shown in figure 1 where f [x(t)]=-x(t) + sin(t) for T = 0.1 sec and x(0) = 2. Fig. 1. Comparison between solution of differential and difference equations. The "to be solved" function: Function f=hwlfunction(t,x,user) % This function is saved as hwlfunction.m f=zeros(1,1) Erstsin(t) The run-time script calling the function: x0=[2] % Initial condition FinalTime=5 % Final Time InitialTime=0 % Initial Time figure(1) options=odeset('reltol,0.001, Stats','on'); [t,y]=ode45(@hwlfunction, [InitialTime FinalTime), [x0]; Plot(t,y(),'m'), hold onStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started