320 likes | 333 Views
Learn the basics of Matlab, including how to start the program, use it as a calculator and matrix calculator, perform matrix operations, find help and other resources, solve numerical ODE and PDE, create GUIs, program with m-files, and create 2D and 3D graphs.
E N D
Quick Introduction to Matlab Dr. Faisal A. Fairag 5 February, 2014
http://faculty.kfupm.edu.sa/math/ffairag/matlabworkshop/index.htmhttp://faculty.kfupm.edu.sa/math/ffairag/matlabworkshop/index.htm Quick Introduction to Matlab Dr. Faisal A. Fairag 5 February, 2014
Quick Introduction to Matlab • How to start Matlab • Matlab as a calculator • Matlab as Matrix Calculator • Matrices and their operations • Using help ( doc, help ) • Other information ( mathworks , central) • Numerical ODE • Numerical PDE • GUI • Programming and ( m-file) • 2D PLOT: plot , ezplot , … • 3D Graph: surf , mesh , contour, ….
What is Matlab? MATLAB – MATrix LABoratory • Initially developed by a lecturer in 1970’s to help students learn linear algebra. • It was later marketed and further developed under MathWorks Inc. (founded in 1984) – www.mathworks.com • Matlab is a software package which can be used to perform analysis and solve mathematical and engineering problems. • It has excellent programming features and graphics capability – easy to learn and flexible. • Available in many operating systems – Windows, Macintosh, Unix, DOS • It has several tooboxes to solve specific problems.
How to start and quit Matlab? PC - a double click on the Matlabicon On both system leave a Matlabsession by typing quit or by typing exitat the Matlab prompt. Use Control ^C to stop the running program. It may take Matlab a while to respond.
Workspace: consists of the variables you create during a MATLAB session; Current Directory browser: shows you where you are. Launch Pad: displays all the tools and applications associated with MATLAB;
Getting Started MATLAB Desktop: • Launch Pad: displays all the tools and applications associated with MATLAB; • Workspace: consists of the variables you create during a MATLAB session; • Command History: double click them to evaluate them; • Current Directory browser: shows you where you are. • Editor/Debugger: pops up when you create an M-files (click on “New” button to launch it.)
The Colon Operator 1:10 is a row vector containing the integers from 1 to 10: 1 2 3 4 5 6 7 8 9 10 Example: To obtain non-unit spacing, specify an increment. For example: 100:-7:50 will give you 100 93 86 79 72 65 58 51
Graphics • MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as editing and printing these graphs. It also includes functions that allow you to customize the appearance of graphics as well as build complete graphical user interfaces on your MATLAB applications.
Graphics Plotting Multiple Data Sets in One Graph Example:
Some Basic Commands pwd prints working directory demodemonstrates what is possible inMatlab who lists all of the variables in your matlab workspace whos list the variables and describes their matrix size clear erases variables and functions from memory clear x erases the matrix 'x' from your workspace ; (semicolon) prevent commands from outputting results % (percent sign) comments line … A line can be terminated with three periods (...), which causes the next line to be a continuation line
Programming: Scripts and Functions Example: solve: a=1;b=-1;c=-2; disc = b^2-4*a*c; r1 = (-b+sqrt(disc) )/(2*a) r2 = (-b-sqrt(disc) )/(2*a)
Programming: Scripts and Functions Example: solve:
Programming: Scripts and Functions Example: File solveq.m function [r1,r2] = solveq(a,b,c) disc = b^2-4*a*c; r1 = (-b+sqrt(disc) )/(2*a); r2 = (-b-sqrt(disc) )/(2*a); solve: You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name
Programming: Scripts and Functions Example: File solveq.m function [r1,r2] = solveq(a,b,c) disc = b^2-4*a*c; r1 = (-b+sqrt(disc) )/(2*a); r2 = (-b-sqrt(disc) )/(2*a); solve:
Programming: Scripts and Functions Huge amount of Matlab function files available in internet About File Exchange File Exchange lets you find and share custom applications, classes, code examples, drivers, functions, Simulink models, scripts, and videos. With File Exchange, you can: Submit files Find files using categories, content types, the search box, and search directives Annotate submissions by adding tags for future lookups, ratings based on your use, and comments that support your ratings Gain community recognition by submitting files that members use, tag, and rate
3D Graph demo matlab graphics Surface Plot of Peaks z=peaks(25); surf(z); colormap(jet); Surface Plot (with Shading) of Peaks z=peaks(25); surfl(z); shading interp; colormap(pink); Slice [x,y,z] = meshgrid(-2:.2:2,-2:.2:2,-2:.2:2); v = x .* exp(-x.^2 - y.^2 - z.^2); slice(v,[5 15 21],21,[1 10]) axis([0 21 0 21 0 21]); colormap(jet) Contour Plot of Peaks z=peaks(25); contour(z,16); colormap(hsv)
Numerical ODE Example: Example: Solve IVP: Solve IVP: Subject to Subject to File solveode.m function [t,x] = solveode t_interval =[0 1]; initial_condition=[1 3 0] [t,x] = ode45(@rhs,t_interval,initial_condition); plot(t,x(:,1),'-',t,x(:,2),'-.',t,x(:,3),'.') legend('x_1','x_2','x_3','Location','eastoutside'); [t,x] function dx = rhs(t,x) dx = zeros(3,1); % a column vector dx(1) = 3*x(1) - x(2) - x(3) + 0; dx(2) = x(1) + x(2) - x(3) + t; dx(3) = x(1) - x(2) + x(3) + 2*exp(t); File rhs.m
Graphical User Interface (GUI) Example: Download two files
Graphical User Interface (GUI) Example: Ladyz.zip