MATLAB Assignment

SAMPLE QUESTION:

Exercise 1 : Consider the functionf(x,C)= sin (C x) C x (a) Create a vector x with 100 elements from -3*pi to 3*pi . Write f as an inline or anonymous function and generate the vectors y1 = f(x,C1) , y2 = f(x,C2) and y3 = f(x,C3) , where C1 = 1 , C2 = 2 and C3 = 3 . Make sure you suppress the output of x and y 's vectors. Plot the function f (for the three C 's above), name the axis, give a title to the plot and include a legend to identify the plots. Add a grid to the plot.

(b) Without using inline or anonymous functions write a function+function structure m-file that does the same job as in part (a) SAMPLE LAB WRITEUP:

MAT 275 MATLAB LAB 1 NAME: __________________________ LAB DAY and TIME:______________ Instructor: _______________________ Exercise 1 (a) x = linspace(-3*pi,3*pi); % generating x vector - default value for number % of pts linspace is 100 f= @(x,C) sin(C*x)./(C*x) % C will be just a constant, no need for ".*" C1 = 1, C2 = 2, C3 = 3 % Using commans to separate commands y1 = f(x,C1); y2 = f(x,C2); y3 = f(x,C3); % supressing the y's plot(x,y1, 'b.-' , x,y2, 'ro-' , x,y3, 'ks-' ) % using different markers for % black and white plots xlabel( 'x' ), ylabel( 'y' ) % labeling the axis title( 'f(x,C) = sin(Cx)/(Cx)' ) % adding a title legend( 'C = 1' , 'C = 2' , 'C = 3' ) % adding a legend grid on Command window output:

f = @(x,C)sin(C*x)./(C*x) C1 = 1 C2 = 2 C3 = 3 (b) M-file of structure function+function function ex1 x = linspace(-3*pi,3*pi); % generating x vector - default value for number % of pts linspace is 100 C1 = 1, C2 = 2, C3 = 3 % Using commans to separate commands y1 = f(x,C1); y2 = f(x,C2); y3 = f(x,C3); % function f is defined below plot(x,y1, 'b.-' , x,y2, 'ro-' , x,y3, 'ks-' ) % using different markers for % black and white plots xlabel( 'x' ), ylabel( 'y' ) % labeling the axis title( 'f(x,C) = sin(Cx)/(Cx)' ) % adding a title legend( 'C = 1' , 'C = 2' , 'C = 3' ) % adding a legend grid on end function y = f(x,C) y = sin(C*x)./(C*x); end Command window output:

C1 = 1 C2 = 2 C3 = 3