User Tools

Site Tools


gibson:teaching:spring-2018:math445:lecture:arithmetic

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gibson:teaching:spring-2018:math445:lecture:arithmetic [2018/01/23 13:35]
gibson [Precedence]
gibson:teaching:spring-2018:math445:lecture:arithmetic [2018/01/24 18:05] (current)
gibson [basic math functions]
Line 77: Line 77:
   j    imaginary unit for electrical eningeers!   j    imaginary unit for electrical eningeers!
  
 +<code matlab>​ 
 +>> 1/0 
 +ans = 
 +   Inf 
 +    
 +>> 0/0 
 +ans = 
 +   NaN 
 +    
 +>> i^2 
 +ans = 
 +    -1 
 +   
 +>> j^2 
 +ans = 
 +    -1 
 +  ​
 >> sin(pi) >> sin(pi)
 ans = ans =
   1.2246e-16 ​ % wha...?   1.2246e-16 ​ % wha...?
 +</​code>​
  
-What does this mean? This is shorthand for scientific notation +What does this mean? This is scientific notation ​shorthand: ''​1.2246e-16'' ​ ​means ​$1.2246 ​\times ​10^{-16}$. So the answer is nearly zero. 
-  ​1.2246e-16 ​ means 1.2246 ​10^(-16)+
  
-Why is sin(pi) not zero? +But why is ''​sin(pi)'' ​not exactly ​zero? Because computers can store and compute only finite truncations of real numbers. Matlab can't represent $\pi$ exactly, only a truncation of $\pi$ that is accurate to sixteen decimal digits.
   ​   ​
 +<code matlab>
 >> 0.4 - 0.3 - 0.1 >> 0.4 - 0.3 - 0.1
 +ans = 
 +   ​2.7756e-17 ​ % wha...? 
 +</​code>​ 
 +Here the issue is that computers use binary representations of numbers, not decimal representations. None of the three numbers 0.4, 0.3, and 0.1 can be represented exactly in binary. They'​re instead represented with binary fractions very nearly equal to 0.4, 0.3, and 0.1. Usually you don't see the difference, but sometimes, like here, you do. 
  
 ====Variables==== ====Variables====
  
 +<code matlab>
 >> x = 4  % assign value of 4 to variable x >> x = 4  % assign value of 4 to variable x
 x =  x = 
Line 108: Line 128:
 ans = ans =
   12   12
 +</​code>​
 ====Evaluating expressions==== ====Evaluating expressions====
 +
 +If you want to evalue the same expression repeatedly with different variables, reset the value of the variables and use the arrow keys to "​scroll up" to the expression. Then hit "​enter"​
 +
 +<code matlab>
 >> x = 3; >> x = 3;
 >> y = x^2 - 2*x + 5 >> y = x^2 - 2*x + 5
Line 119: Line 143:
 y = y =
     4     4
 +</​code>​
 ==== basic math functions ==== ==== basic math functions ====
-  ​sin, cos, tan, sec, csc, cot, asin, acos, atan,  + 
-  exp, log, log10, abs, sqrt, factorial, mod+<code matlab>​ 
 +  ​sin, cos, tan, sec, csc, cot, asin, acos, atan          % the classic trig funcsin radians 
 +  sind, cosd, tand, secd, cscd, cotd, asind, acosd, atand % the classic trig funcs, in degrees 
 +  exp, log, log10, abs, sqrt, factorial, mod              % other awesome functions 
 +</​code>​ 
   ​   ​
   ​   ​
gibson/teaching/spring-2018/math445/lecture/arithmetic.1516743340.txt.gz · Last modified: 2018/01/23 13:35 by gibson