MATLAB Programs
You should search online for the basic of MATLAB if you haven't heard it before.
Just copy the program below
and paste it in MATLAB command window.
You only have to change the red colored text to finish the related
part of the homework.
You can copy line by line or the whole text. Add comments by beginning with %(All
comments are green in the following code to help you understand it).
Blue colored text are optional parameters, you can change it to see the effects.
Direction Fields
The following code is for probelm 22, in section 2.1.
t=linspace(-2,10,10); % Define the t region. Here 10 means we plot the direction fields
for ten points.
y=linspace(-4,4,10); % Define the y region
[T,Y]=meshgrid(t,y); % Get the grid matrices
U=ones(size(T)); % Find the horizontal component of the direction fields
V=Y.^2-T;
% Find the vertical component of the direction
fields
% Put a dot for any multiplication, division or power, because you want to perform 'elementwise calculation'
instead of 'matrxi calculation'.
L=sqrt(U.^2+V.^2);U=U./L;V=V./L; % Normalize the direction fields to be the same
length
quiver(t,y,U,V,0.5); % plot the direction
field, 0.5 mean the vector are rescaled to half of the original size
Numerical solver.
We use Problem 27, Section 2.2 as an example(y'-3y=sin t, y(0)=-3).
[t1,y1]=ode23(@(t,y)(3*y+sin(t)),[0 pi/4],-3);
% Solve the solution on the interval [0,
pi/4]
[t2,y2]=ode23(@(t,y)(3*y+sin(t)),[0 -6*pi],
-3); % Solver the solution on the interval
[-6*pi, 0]
t=[t2(length(t2):-1:1); t1]; % Merge the indenpendent variable t
y=[y2(length(y2):-1:1); y1]; % Mearge the dependent variable y
plot(t,y) % Plot the solution
Remarks:
Put the equation in to normal form first.
Find the solution on the positive interval ([0 pi/4]) first, and then on the negative
interval([0 -6*pi]), because I meage the solution with this in mind.
Put brackets for the definition of the function (sin(t) in above example instead of
(sin t) if possible.
Rerun ALL the subsequent codes again to update the output, if you
make the change somewhere.