80 likes | 90 Views
SE207 Lab 2. Objective: To be able to create script and function files. MATLAB Files. Two types of files: SCRIPT files FUNCTION files Both have the extension .m [ M-files] (file1.m, f2.m). SCRPT Files: a list of MATLAB instructions saved on a file for convenience no input argument
E N D
SE207 Lab 2 Objective: To be able to create script and function files
MATLAB Files • Two types of files: • SCRIPT files • FUNCTION files • Both have the extension .m [ M-files] (file1.m, f2.m)
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 M-files t=0:0.1:10 y=sin(t) plot(t,y) function [y]=fplot2(t) y=sin(t) plot(t,y)
Function M-files • create an m-file and save it as fplot.m • MATLAB Command Window FILE New M-file function y=fplot(t) y=sin(t); Open M-file >>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 statements end If (x>0) f=x^2 else f=0 end
for loops General form: 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