Objective: To be able to create script and function files SE207 Lab 2 Objective: To be able to create script and function files
MATLAB Files Two types of files: Both have the extension .m [ M-files] SCRIPT files FUNCTION files Both have the extension .m [ M-files] (file1.m, f2.m)
M-files t=0:0.1:10 y=sin(t) plot(t,y) function [y]=fplot2(t) y=sin(t) SCRPT Files: a list of MATLAB instructions saved on a file for convenience no input argument The variables are global FUNCTION files Starts with “FUNCTION “ statement that specifies the input and output arguments Local variables Treated as the built-in functions t=0:0.1:10 y=sin(t) plot(t,y) function [y]=fplot2(t) y=sin(t) plot(t,y)
Function M-files function y=fplot(t) y=sin(t); create an m-file and save it as fplot.m FILE New M-file function y=fplot(t) y=sin(t); Open M-file MATLAB Command Window >> t=0:0.1:10 >> z=fplot(t) >> plot(t,z)
MATRIX OPERATIONS Transpose A’ inverse inv(A) determinant det(A) rank rank(A)
Special functions Exponential exp(x) square root sqrt(x) natural logarithm log(x) common logarithm log10(x) absolute value abs(x) more details Use help to know related functions
If structures General form: If condition statements else end If (x>0) f=x^2 else f=0 end
for loops General form: special case for index=initial: limit for index=initial: increment: limit statements end special case if increment is 1 for index=initial: limit for i=1:100 x(i)=sin(i*.2) end