50 likes | 327 Views
Input and Output Algebra. Arithmetic Errors in Input. MATLAB Basics. Yun Suk Jang Department of physics Kangwon National University. Input and Output. You input commands to MATLAB in the MATLAB command Window. MATLAB returns output in two ways: Text.
E N D
Input and Output Algebra Arithmetic Errors in Input MATLAB Basics Yun Suk Jang Department of physics Kangwon National University
Input and Output • You input commands to MATLAB in the MATLAB command Window. MATLAB returns output in two ways: • Text. • Numerical output is returned in the same Command Window. Arithmetic • You can use MATLAB to do arithmetic as you would a calculator: (1) “ + ” to add, “ - ” to subtract, “ * ” to multiply, “ / “ to divide, “ ^ ” to exponentiate. Ex) >> 3^3 – (5 + 4) / 2 + 6*3 Ans = 22.5000 (2) Other Function : sqrt, cos, sin, tan Ex) >> ans^2 + sqrt ( ans ) Ans = 510.9934
To perform symbolic computations, use syms to declare the variables you plan to use to be symbolic variables. >> syms x y >> (x – y)*(x – y)^2 ans = (x – y)^3 The command expand MATLAB to multiply out the expression, and factor forced MATLAB to restore it to factored form. >> expand (ans) ans = x^3 – 3*x^2*y + 3*x*y^2 – y^3 >> factor (ans) ans = (x-y)^3 The command called simplify, which you can sometimes use to express a formula as simply as possible. >> Simplify ((x^ - y^3) / (x – y)) ans = x^2 + x*y + y^2 Calculation with the Symbolic Expression. >>cos(pi/2) ans = 6.1232e-17 -> However, we Know that cos(/2) is really equal to 0. >> cos(sym(‘pi / 2’) ans = 0 -> Symbolic representation of /2 You can also do variable-precision ariyhmetic with vpa. >> vpa(‘sqrt(2)’, 30) ans = 1.41421356237309504880168872420 Algebra
Errors in Input • If you make an error in an input line, MATLAB will beep and print an error message. Ex) >> 3u^2 ??? 3u^2 error: missing operator, comma, or semicolon. • The error is a missing multiplication operator *. The correct in put would be 3*u^2. Note that MATLAB places a marker at the place where it think the error might be; however, the actual error may have occurred earlier or later in the expression.