Homework # 1 Due Jan 25 in class

You can create your own matrices using M-files, which are text files containing MatLab code. Just create a file containing the same statements you would type at the MatLab command line. Save the file under a name that ends in .m. As a practice, you may create an M-file and save it on your diskette.

Note: You must turn in

(1) a labeled, virus free disk with directory hw# containing all the files relevant to the assignment, including *.m (if you are using MatLab). Put all commands in at most 3 different m-files. I want to be able to run your m-files and see all output appear on the screen. If you'd like, you can put all command in one file. However you need to use the command figure in front of the last plot. This way your output will give 2 windows one for each plot.

(2) a hard copy of the output.

On invoking MatLab, you will see the symbol ">>" in the command window. Enter the command x= [10.1; 20.2; 30.3]. A column vector is produced. Matlab will displace the consequence of a command unless it is terminated with a semicolon. Enter

x= [10.1; 20.2; 30.3]; Then observe the result.

Now try C = [3 5 8 1; 1 0 3 6; 4 3 3 8; 7 8 1 7; 2 4 1 0; 8 2 1 9] followed by

y = C*[2; 3; 7; 5]. This gives the product of two matrices. You can use the help command to find some other operations. For example, you may try to figure out the transpose of C and inverse of C.

Define the matrices A and B as




Compute AB and BAT where AT is the transpose of A.

One of the real strengths of MatLab lies in its ease in plotting both 2D and 3D functions. Let us consider plotting f(x) = sin(2x) on [0,1]. In MatLab, there are three components to this task.

Let's try the following. x = 0:.05:1; y = sin(2**x); plot(x,y)

You need to print the graph you obtained.

Write an M-file to plot the function:

f(x) = 2sin(x) + 3sin(2x) + 7sin(3x) + 5sin(4x), x [-10,10]

You may use the following code.

n = 20;

x = linspace(-10,10,n)';

y = zeros(n,1);

for k = 1:n

y(k) = 2*sin(x(k)) + 3*sin(2*x(k)) + 7*sin(3*x(k)) + 5*sin(4*x(k))

end

plot(x,y)

title('f(x)= 2sin(x) + 3sin(2x) + 7sin(3x) + 5sin(4x)')

  1. MatLab can plot several functions together. Let's consider the following code (xcreate an M-file for it) to graph

f(x) = 2sin(x) + 3sin(2x) + 7sin(3x) + 5sin(4x)

g(x) = 8sin(x) + 2sin(2x) + 6sin(3x) + 9sin(4x)

over [-10,10]:

close all; clc

n = 200;

x = linspace(-10,10,n)';

A= [sin(x) sin(2*x) sin(3*x) sin(4*x)];

y = A*[2 8; 3 2; 7 6; 5 9];

plot (x,y)