% By default, Matlab and Octave display only a limited % number of digits. To display approximately 16 decimal % digits, use 'format long'. To return to the default % display format, use 'format'. format long % Functions in Matlab and Octave are called directly by % their names; no package names or namespaces are required. % Mathematically, these three expressions are equal: % % (5 + 2*sqrt(7))(5 - 2*sqrt(7)) % = 5^2 - (2*sqrt(7))^2 % = 25 - 28 % = -3. % % Numerically, however, the first result may differ slightly % from -3 because sqrt(7) cannot be represented exactly as a % floating-point number. (5 + 2*sqrt(7))*(5 - 2*sqrt(7)) 5*5 - 2*2*7 5^2 - 2^2*7 % ^ denotes exponentiation printf( '\nRoots of a polynomial with real roots:\n' ) % Find the roots of the polynomial % % 2 % 3.8x + 4.2x - 1.7. % % For % % 2 % ax + bx + c = 0, % % the quadratic formula gives % % 2 % -b +/- sqrt(b - 4ac) % x = ---------------------. % 2a a = 3.8 b = 4.2 c = -1.7 % Be sure to put parentheses around the denominator. % For example, % % (-b - sqrt(b^2 - 4*a*c))/2*a % % is interpreted as % % ((-b - sqrt(b^2 - 4*a*c))/2)*a, % % which is not the quadratic formula. (-b - sqrt(b^2 - 4*a*c))/(2*a) (-b + sqrt(b^2 - 4*a*c))/(2*a) printf( '\nArithmetic with the imaginary unit:\n' ) % Matlab and Octave use both 'i' and 'j' for the imaginary % unit. However, either name can be reassigned as a variable. % Writing 1j explicitly represents the imaginary unit even % if the variable j has been assigned another value. % % Parentheses are required when raising 1j to a power: % write (1j)^5 rather than 1j^5. (1j)^5 (5 + 2j)*(5 - 2j) 5*5 - 2*2*(1j)*(1j) 5^2 - (2j)^2 printf( '\nRoots of a polynomial with complex roots:\n' ) % Now find the roots of the polynomial % % 2 % 3.8x + 4.2x + 1.7. % % Here the discriminant is negative, so the two roots % are a complex-conjugate pair. c = 1.7 (-b - sqrt(b^2 - 4*a*c))/(2*a) (-b + sqrt(b^2 - 4*a*c))/(2*a)