Mat 275 Modern Differential Equations

Mat 275 Modern Differential Equations

Dr. Firozzaman

Department of Mathematics and Statistics

Arizona State University

Lab 2 due on August 1, 2006

Examples: Try all

(Do not print, you need to print solutions of the homework problems only)

Files containing MATLAB commands are called m-files and have a .m extension. They are two types:

a)  A script is simply a collection of MATLAB commands gathered in a single file. The value of the data created in a script is still available in the command window after execution.

Example 1. Script file save as plot1.m (type as a file and save)

Plotting a function using window [0, 5]x[-5, 10]

x = 0:1:5;

y = 22*exp(x./5)-5*x-25;

plot(x,y,’r-’, ’LineWidth’,2);

axis([0,5,-5,10]); grid on;

title(’f(x)=(22 e^(x/5)-5x-25)’);

xlabel(’x’); ylabel(’y’);

Then run your program and get the plot on MATLAB prompt by typing >plot1

b)  A function is similar to a script, but can accept and return arguments. Unless otherwise specified any variable inside in a function is local to the function and not available in the workspace. A function invariably starts with the command

function output = function_name(input)

Example 2. Script + Function (two separate files)

i)  Save the file as function1.m

function y=function1(x)

y = 22*exp(x./5)-5*x-25;

ii)  Save the file as plot2.m

x = 0:1:5;

y = feval(@function1,x);

plot(x,y,’r-’, ’LineWidth’,2);

axis([0,5,-5,10]); grid on;

title(’f(x)=(22 e^(x/5)-5x-25)’);

xlabel(’x’); ylabel(’y’);

Then run your program and get the plot on MATLAB prompt by typing >plot2

c)  Script + Function (one file) save as plot3.m

function plot3

x = 0:1:5;

y = feval(@function1,x);

plot(x,y,’r-’, ’LineWidth’,2);

axis([0,5,-5,10]); grid on;

title(’f(x)=(22 e^(x/5)-5x-25)’);

xlabel(’x’); ylabel(’y’);

%------

function y=function1(x)

y = 22*exp(x./5)-5*x-25;

Then run your program and get the plot on MATLAB prompt by typing >plot3

d)  Numerical solution by Euler Method

As an example consider the initial value problem (IVP)

Follow the steps:

>clear t y

>y(1)=3; t(1)=0; h =0.1;

>f=inline(’2*y’, ’t ’, ’y ’);

>y(2)=y(1)+h*f(t(1),y(1)), t(2)=t(1)+h,

>y(3)=y(2)+h*f(t(2),y(2)), t(3)=t(2)+h,

>y(4)=y(3)+h*f(t(3),y(3)), t(4)=t(3)+h,

>y(5)=y(4)+h*f(t(4),y(4)), t(5)=t(4)+h,

>y(6)=y(5)+h*f(t(5),y(5)), t(6)=t(5)+h,

>[t(:),y(:)]

>plot(t,y);axis tight

------

Homework: You need to print your work and turn in.

1.  Write an m-file to plot the function with suitable x and y values

2.  Use Euler Method to plot the solution curve of the IVP

3.  Find eigenvalues and eigenvectors of A = magic(3) (Optional)

------

Try the following matrix operations. Do not print.

> A=[8, 1,6; 3, 5, 7; 4, 9, 2]

> row1=[8, 1, 6]; row2 =[3, 5, 7]; row3 =[4, 9, 2]; A=[row1; row2; row3]

Also try using columns like co1, col2, col3

Try the following and understand what you get back:

>A(2, 3) % coefficient of A in 2nd row , 3rd column

>A(1,:) % first row of A

>A(:,3) % 3rd column of A

>I=eye(3)

>B=magic(3) %can you find what is magic of this matrix?

>C= A+B

>D = A*B

>[S,D]= eig(A)

%determines the eigenvectors of A (columns of S) and associated eigenvalues (diagonal coefficients of D) note that the eig function has one input and two outputs arguments.

You may verify eigenvalues and eigenvectors of the problem we have solved in the class (Section 5.2)

------

HAVE FUN

------