DOC PREVIEW
CORNELL CS 404 - MATLAB

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1OutlineOutline• Announcements– HWI due today, 5PM– HWII available later today– Sign-up for CTC account• MATLAB• Bringing MATLAB into CMATLABMATLAB• “MATrix LABoratory”• Started out as a front end for LINPACK, alinear algebra library that was the antecedentof LAPACK• MATLAB is an interactive system for scientificcomputing– lots of built-in functions– programmable– excellent graphics• But what is MATLAB?This isThis is……2MATLAB basicsMATLAB basics• Interact with MATLAB through acommand line• Commands are evaluated one-by-one• MATLAB stores variables in the“workspace”– Can get info on variables by typing “whos”MATLAB variablesMATLAB variables• MATLAB variables are 2D arrays ofdoubles (e.g. matrices)– Through V4, this was strictly true– V5 added ND-arrays, structs, and cell-arraysMATLAB arraysMATLAB arrays• Can create arrays with– brackets• A=[1 3 5;2 4 6];– commands like zeros, ones, rand• A=zeros(2,3);– colon operator• A=[1:2:5; 2:2:6];– or by loading a file• A=load(‘Cfinal.txt’);3MATLAB operationsMATLAB operations• Arithmetic and logical operations inMATLAB are vectorized:– A=B+C is equivalent tofor(j=0;j<nrows;j++){for(k=0;k<ncols;k++){A(j,k)=B(j,k)+C(j,k);}}• B and C must be same size or scalar– A=B*C is matrix multiplication• B: m-by-p, C: p-by-n, then A: m-by-nMATLAB operationsMATLAB operations• MATLAB’s “\” solves linear systems– x=A\b; %solves Ax=b– “\” is very smartMATLAB functionsMATLAB functions• MATLAB provides lots of built-infunctions– Math: sin, cos, sqrt– Lin. Alg: eigs, svd, lu– Solve ODE’s– These are all vectorized• There are many toolboxes whichcontain additional functions4MATLAB programmingMATLAB programming• Can create new functions (m-files)– text files ending with .m & containingMATLAB commands– first line of file has form:• function [o1, o2,… oM]=name(i1,i2,… iN)Inputs -call-by-valueOutputs -value of o1, o2,etc at end will be returnedMATLAB ProgrammingMATLAB Programmingfunction [C]=SolveA(A,RHS);[m,n]=size(A);[p,q]=size(RHS);if(p!=m)error(‘RHS &A must have same number of rows’);endC=A\RHS;• MATLAB programs are simple and compact• Excellent language for prototypingCalling MATLABCalling MATLAB• C/C++ programs can make use ofmany of MATLAB’s nice features:– vectorized operations– complex types– linear algebra and math functions5Calling MATLABCalling MATLAB• Bring these capabilities to C was noteasy– need to manage memory in a MATLAB-likeway– need to call functions in a MATLAB-like wayUsing MATLAB C LibraryUsing MATLAB C Library• #include “matlab.h”--Matlab C-functions andtypes• Declare variables used with MATLAB functionsto be– mxArray *volatile name=NULL;• Start MATLAB memory management– mlfEnterNewContext(nout,nin,outvars,invars);• nout,nin--number of MATLAB output/input variables• outvars--list of output variables• invars--list of input variables• for main, this is just (0,0)• Write your code• Destroy mxArray variables:– mlfDestroyArray(name);• Stop MATLAB memory management– mlfRestorePreviousContext(nout,nin,outvars,invars)Using MATLAB C LibraryUsing MATLAB C Library6Using MATLAB C LibraryUsing MATLAB C Library• Create with mbuild:– mbuild matccode.c– Passes matccode.c to C-compiler, and links toappropriate libraries• MATLAB Libraries– MATLAB C functions are included in several “Shared-Object” (.so) libraries– Will talk more about them on Wed.– Must add <matlab>/extern/lib/<arch> toLD_LIBRARY_PATH• setenv LD_LIBRARY_PATH/usr/local/matlab/extern/lib/sgi:/usr/local/matlab/extern/lib/sgi/bin/sgi:$LD_LIBRARY_PATHUsing MATLAB C LibraryUsing MATLAB C Library• A=<Matlab expression>– mlfAssign(&A,mlf expression)– Ex: A=B*C;• mlfAssign(&A,mlfMtimes(B,C))• Ex: [L,U]=lu(A);• Matlab function can take 1-2 inputs andproduce up to 3 outputs• C-version provides this too– mlfAssign(&L,mlfLu(&U,NULL,A,NULL));Using MATLAB C LibraryUsing MATLAB C Library7Mixing Simple C and MATLABMixing Simple C and MATLABCC• Converting C-arrays to mxArrays:– mlfAssign(&MXarray,mlfDoubleMatrix(m,n,Carray,NULL))– Copies contents of Carray (or first m*nelements) into Mxarray– For 1D arrays, this is straight-forward, but:Multidimensional ArraysMultidimensional Arrays654321654321635241A=C: Row-major MATLAB: Column-majorMultidimensional ArraysMultidimensional Arrays654321mlfAssign(&Marray, mlfDoubleMatrix(2,3,Carray,NULL))Carray Marray642531654321NOT:8Mixing Simple C and MATLABMixing Simple C and MATLAB• Converting from mxArrays to C– Get pointer to data in mxArray• double ptr;• ptr=mlfGetPt(A);– Use C function “memcpy”• memcpy(C,ptr,m*sizeof(double));– m is the amount of data in A to copy– m=prod(size(A));• MATLAB archives data in an efficient filetype called .mat• We can read and write to .mat filesfrom C– mlfSave(mxCreateString(”name.mat"),"w",• ”o1",o1,”o2",o2,…,NULL);– mlfLoad(mxCreateString(“name.mat”),• “i1”,i1,”i2”,i2,…,NULL);Working with .mat filesWorking with .mat


View Full Document

CORNELL CS 404 - MATLAB

Download MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?