You are a guest. Restricted access. Read more

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

jmathlab:arithmetic [2012/11/10 22:36] admin |
jmathlab:arithmetic [2015/05/10 17:18] (current) |
||
---|---|---|---|

Line 1: | Line 1: | ||

+ | ====== Arithmetic ====== | ||

+ | {{indexmenu_n>10}} | ||

+ | ~~NOCACHE~~ | ||

+ | |||

+ | |||

+ | First, we will learn how to print a variable which keeps a number. In our example, the variable "x" is assigned to 1000 | ||

+ | |||

+ | <jc math> | ||

+ | x=1000; printf('%f',x) | ||

+ | </jc> | ||

+ | Click [Output] to see what you are just printed on the screen. Numbers are entered in the usual computer format: with optional decimal point and decimal exponent following the letter e (or E). The numbers 5364 and | ||

+ | $-1.723478265342 10^{12}$ should be entered like | ||

+ | |||

+ | |||

+ | The basic arithmetic operations are marked with the usual symbols (+ - * / ) . Exponention is performed with the accent character (^). Multiplication and division precede addition and subtraction; any order of evaluation can be forced by parenthesis. | ||

+ | |||

+ | |||

+ | |||

+ | <html> | ||

+ | <center><table BORDER=4 > | ||

+ | <tr> | ||

+ | <td> + </td> | ||

+ | |||

+ | <td>addition</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td> - </td> | ||

+ | |||

+ | <td>subtraction</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td> * </td> | ||

+ | |||

+ | <td>multiplication</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td> / </td> | ||

+ | |||

+ | <td>division</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td> \ </td> | ||

+ | |||

+ | <td>left division</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td> ^ </td> | ||

+ | |||

+ | <td>power</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td> ' </td> | ||

+ | |||

+ | <td>conjugate transpose</td> | ||

+ | </tr> | ||

+ | </td> | ||

+ | </table> | ||

+ | </center> | ||

+ | </html> | ||

+ | |||

+ | |||

+ | |||

+ | |||

+ | |||

+ | |||

+ | |||

+ | |||

+ | |||

+ | |||

+ | |||

+ | <jc math> | ||

+ | a=3.23*(14-2^5)/(15-(3^3-2^3)); printf('%f\n',a) | ||

+ | a=4.5e-23/0.0000013; printf('%f\n',a) | ||

+ | a=17.4^((3-2.13^1.2)^0.16); printf('%f\n',a) | ||

+ | a=17.23e4/(1.12-17.23e4/(1.12-17.23e4/1.12)); printf('%f\n',a) | ||

+ | </jc> | ||

+ | |||

+ | |||

+ | |||

+ | ===== Relations. ===== | ||

+ | |||

+ | |||

+ | The relational operators | ||

+ | |||

+ | <html> | ||

+ | <center><table BORDER=4 > | ||

+ | <tr> | ||

+ | <td><</td> | ||

+ | |||

+ | <td>less than</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td>></td> | ||

+ | |||

+ | <td>greater than</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td><=</td> | ||

+ | |||

+ | <td>less than or equal</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td>>=</td> | ||

+ | |||

+ | <td>greater than or equal</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td>==</td> | ||

+ | |||

+ | <td>equal</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td>~=</td> | ||

+ | |||

+ | <td>not equal.</td> | ||

+ | </tr> | ||

+ | </table></center> | ||

+ | </html> | ||

+ | |||

+ | Note that "=" is used in an assignment statement while "==" is used in | ||

+ | a relation. Relations may be connected or quantified by the logical operators | ||

+ | |||

+ | <html> | ||

+ | <center><table BORDER=4 > | ||

+ | <tr> | ||

+ | <td>&</td> | ||

+ | |||

+ | <td>and</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td>|</td> | ||

+ | |||

+ | <td>or</td> | ||

+ | </tr> | ||

+ | |||

+ | <tr> | ||

+ | <td>~</td> | ||

+ | |||

+ | <td>not.</td> | ||

+ | </tr> | ||

+ | </table></center> | ||

+ | </html> | ||

+ | |||

+ | When applied to scalars, a relation is actually the scalar 1 or 0 depending | ||

+ | on whether the relation is true or false. | ||

+ | |||

+ | Logical true is the number 1, false is 0. | ||

+ | |||

+ | Example: | ||

+ | |||

+ | <jc math> | ||

+ | A=1;B=1;C=1; % semicolon suppresses output. | ||

+ | a= !(A&B)|(B&C) == (C~=A) | ||

+ | printf('%f',a) | ||

+ | </jc> | ||

+ | |||

+ | |||

+ | Also, eps is defined: | ||

+ | |||

+ | <jc math> | ||

+ | a=eps; printf('%f\n',a) | ||

+ | a=1+eps>1; printf('%f\n',a) | ||

+ | a=1+eps/2>1; printf('%f\n',a) | ||

+ | </jc> | ||

+ | |||

+ | |||

+ | Most of the time these will be stored as floating point data (double, IEEE standard 754). They are rounded to 5 significant digits for display, but for calculations the full precision of this format is always preserved (15-16 decimal digits). By switching the format (format long) all significant places are displayed. | ||

+ | |||

+ | <jc math> | ||

+ | format long | ||

+ | x=-1.723478265342e12; printf('%f',x) | ||

+ | </jc> | ||

+ | |||

+ | As an extension it offers the command format Base Number, which is used to display numbers in a system with arbitrary Base with any Number of significant digits. To display numbers with $15$ digits in the binary system we type: | ||

+ | |||

+ | <jc math> | ||

+ | format 2 15 | ||

+ | x=-1.723478265342e12; printf('%f',x) | ||

+ | </jc> | ||

+ | Using format short returns the display mode to default (short decimal). It should be emphasized, that none of the format commands influences the internal representation and accuracy of floating point numbers. | ||

+ | |||

+ | Numbers, which are entered without decimal point and exponent, and which are larger than <m>10^{15}</m> are stored as exact rational data type. These numbers are internally represented as quotient of two variable length integers (java data type BigInteger), which allows you to perform calculations without any rounding errors. In the first case of the following example a floating point number is generated, in the second case an exact rational: | ||

+ | |||

+ | <code> | ||

+ | 10000000000000001. | ||

+ | 10000000000000001 | ||

+ | </code> | ||

+ | |||

+ | Each floating point number Z can be converted to an exact number using the command rat(Z). The conversion is accomplished by continued fraction expansion with an accuracy determined by the variable ratepsilon (default: $10^{-8}$). | ||

+ | |||

+ | <jc math> | ||

+ | x=rat(0.33333333333333333); printf('%f',x) | ||

+ | </jc> | ||

+ | The rat() function is a part of symbolic calculation; in such cases we | ||

+ | will need to specify "syms". You can comment it out, but the code should contain the | ||

+ | keyword to let the engine know that we are interested in symbolic evaluation. | ||

+ | |||

+ | Operations between exact and floating point numbers always lead to the promotion of floating point numbers. Calculations can be performed without rounding errors by rationalizing just the first number. | ||

+ | |||

+ | <jc math> | ||

+ | x=1/21/525/21/5*7*175*63*15-1; printf('%f',x) | ||

+ | </jc> | ||

+ | |||

+ | <jc math> | ||

+ | x=rat(1)/21/525/21/5*7*175*63*15-1; printf('%f',x) | ||

+ | </jc> | ||

+ | |||

+ | Conversely, the command float(Z) converts numbers into floating point format. Both commands also work for composite data types, like polynomials and matrices, whose coefficients are transformed in one step. Irrational function values of exact numbers and constants like pi remain non-evaluated until the float-command is issued. | ||

+ | |||

+ | <jc math> | ||

+ | x=sqrt(2); printf('%f\n',x) | ||

+ | x=sqrt(rat(2)); printf('%f\n',x) | ||

+ | x=float(10); printf('%f\n',x) | ||

+ | </jc> | ||

+ | |||

+ | You can also use comments in the output: | ||

+ | |||

+ | <jc math> | ||

+ | x=log(sqrt(854)); printf('Answer=%f', x); | ||

+ | </jc> | ||

+ | |||

+ | Imaginary numbers are marked with an immediately following i or j. This will work even if the predefined variables i and j have been overwritten. | ||

+ | <jc math> | ||

+ | x=2+3i | ||

+ | printf('number=%f', x); | ||

+ | </jc> | ||

+ | |||

+ | ;;# | ||

+ | [[:jmathlab:variables |{{::next.png}}]] | ||

+ | ;;# | ||