60 likes | 75 Views
MatLab Basics. Jae Hoon Kim Department of Physics Kangwon National University. • It contains hundreds of commands to do mathematics. • Graph functions, solve equations, perform statistical tests • High level programming language that can communicate
E N D
MatLab Basics Jae Hoon Kim Department of Physics Kangwon National University • It contains hundreds of commands to do mathematics. • Graph functions, solve equations, perform statistical tests • High level programming language that can communicate with the other programming language such as FORTRAN and C.
MatLab Desktop Double click the MatLab icon on the Windows Desktop → Launch the MatLab → MatLab Desktop appears: Workspace Display the information for the defined variables Command window Enter the command and Output the result Command History List of used commands
Arithmetic • Simple Arithmetic (1) Four fundamental operations : +, -, *, / >> 3^2-(5+4)/2+6*3 ans= 22.5000 (2) Mathematic Functions: sqrt, cos, sin, tan >>ans^2+sqrt(ans) ans= 510.9934 • Precision (1) MATLAB uses double-precision floating point arithmetic. -> 15 digits (2) MATLAB display only 5 digits for the output result by default. Format long -> display 15 digits Format short -> return to 5-digit display
• To perform symbolic calculation, use “syms” command to declare the symbolic variable. >> syms x y >> (x-y)*(x-y)^2 ans = (x-y)^3 • Commands for the symbolic calculation (1) expand: multiply out the expression >> expand(ans) ans = x^3-3*x^2*y+3*x*y-y^3 (2) factor: restore to factored form >> factor(ans) ans = (x-y)^3 (3) simplify: express a formula as simply as possible. >> simplify((x^3-y^3)/(x-y)) ans = x^2+x*y+y^2 • Calculation with the Symbolic Expression >>cos(pi/2) -> 6.1232e-17 (not 0) Because “pi” gives an approximation to accurate to about 15 digits, not its exact value. >>cos(sym(‘pi/2’)) ->0 Symbolic representation of /2 -> exact value. • Variable – precision Arithmetic >>vpa(‘sqrt(2)’, 50) print 50 digits of √ 2 Algebraic and Symbolic Calculation
Managing Variables • Data type (1) floating point numbers (2) symbolic expressions (3) strings • “whos” command: display a summary of the names and types of currently, defined variables. (Workspace browser presents a graphical alternative to “whos”) • “clear” or “clear all” : Clear all defined variables
Online Help To get help on a command, enter help followed by the name of the command. ex) help solve: print out the online help for “solve” command. •more on: Display the online help. One screen-full at a time To search the first line of every online help for a specified string use “lookfor” command ex) lookfor factor: Display the help that contain the word “factor” as part of the command name of brief description