110 likes | 238 Views
MATLAB Programming Chapter 2. MATLAB. a flagship software which was originally developed as a matrix library . A variety of numerical functions, symbolic computations, and visualization tools have been added to the matrix manipulations. Demo programs:
E N D
MATLAB Programming Chapter 2
MATLAB a flagship software which was originally developed as a matrix library. A variety of numerical functions, symbolic computations, and visualization tools have been added to the matrix manipulations. Demo programs: http://web.mst.edu/~ercal/228/MATLAB/1-2/analpara.m http://web.mst.edu/~ercal/228/MATLAB/1-2/analpara2.m http://web.mst.edu/~ercal/228/MATLAB/1-2/analpara4.m % file I/O http://web.mst.edu/~ercal/228/MATLAB/1-2/numpara.m http://web.mst.edu/~ercal/228/MATLAB/1-2/numpara2.m
Sample Program • g=9.8; • cd=12.5; • m = 68.1; • dt = input('time increment (s):'); • tf = input('final time (s):'); • ti=0; • vi=0; • while (1) • dvdt = g-(cd/m)*vi; • vi = vi + dvdt*dt; • ti = ti + dt; • if ti >= tf, break, end • end • disp('velocity (m/s):') • disp(vi) m=68.1 kg; c=12.5 kg/s; g=9.8 m/s
Built-in functions Display formats Rounding functions format short : 41.4286 formatlong: 41.42857142857143 format short e: 4.1429e+001 formatlong e: 4.142857142857143e+0001 format short g: 41.429 formatlong g: 41.4285714285714 formatbank: 41.43 format compact: eliminatesemptylines formatloose: addsemptylines round(x) fix(x) - round towardszero ceil(x) floor(x) rem(x,y) – returnstheremainderafter x isdividedby y (similar to % function in C) sqrt(x) exp(x) abs(x) log(x) log10(x) factorial(x) TRIGONEMETRIC sin(x) sind(x) cos(x) cosd(x) tan(x) tand(x) cot(x) cotd(x)
Plotfunction >> x=0:1:5 x = 0 1 2 3 4 5 >> y = sin(10*x) + cos(3*x) y = 1.0000 -1.5340 1.8731 -1.8992 1.5890 -1.0221 >> plot(x,y) >> xlabel('x in radians') >> ylabel('y = sin(10*x) + cos(3*x)') >> >> z = zeros(1,6) % alternativeway z=0*x z = 0 0 0 0 0 0 >> plot(x,y, x,z) >> xlabel('x in radians') >> ylabel('y = sin(10*x) + cos(3*x)')
Roots of polynomials System of equations >> x=[-1, 5] x = -1 5 >> A = [2, 3; -1, 4] A = 2 3 -1 4 >> b = A*x' b = 13 21 >> solveX = inv(A)*b solveX = -1.0000 5.0000 >> r = [1, -2, 4] r = 1 -2 4 >> poly(r) ans = 1 -3 -6 8 >> p = poly(r) p = 1 -3 -6 8 >> solve = roots(p) solve = 4.0000 -2.0000 1.0000
Passing a function as an argument The following program plots a function (passed as an argument) between a specified range (xa, xb): http://web.mst.edu/~ercal/228/MATLAB/1-2/myplot.m function draw = myplot(func, xa, xb, inc, LabelX, LabelY) X = xa:inc:xb Y = func(X); Z = 0*X; plot(X,Y,X,Z); xlabel(LabelX); ylabel(LabelY); function f1 = myfunc(x) % stored in another file called myfunc.m f1 = sind(x); MATLAB call: >> myplot(@myfunc, xmin, xmax, increment, LabelX, LabelY)
Fundamental control structures in MATLAB FOR-Loopsum = 0; DOFOR i = start, step, final for i = 2:1:25 (LoopBody) sum = sum + A[i]; ENDDO end Managing Variables clear – removesall variables frommemoryclear x y – removesonly x and y frommemory who – displays a list of variables in thememorywhos – displays a list of variables in the memoryalongwiththeirsize and class
EXCEL • Spreadsheet that allows the user to enter and perform calculations on rows and columns of data. • When any value on the sheet is changed, entire calculation is updated, therefore, spreadsheets are ideal for “what if?” sorts of analysis. • Excel has some built in numerical capabilities including equation solving, curve fitting and optimization. • It has several visualization tools, such as graphs and three dimensional plots. • It also includes Visual Basic (VBA) as a macro language that can be used to implement numerical calculations. check out: http://www.anthony-vba.kefra.com/vba/vbabasic1.htm