User Tools

Site Tools


gibson:teaching:fall-2013:math445:lecture3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Scientific notation shorthand

% Matlab and other programming languages use a shorthand for scientific notation

4.1 * 10^13
ans =  4.1000e+13

% You can also type that shorthand in directly
4.1e13
ans =  4.1000e+13

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% You can save and load scalars, vectors, and matrices to disk as follows

A = [1 3; -2 7]
A =

   1   3
  -2   7

save -ascii -double A.asc A  % save matrix A to file A.asc
clear all                    % clear all variables from memory

load A.asc                   % load data from file A.asc, store in variable A
A
A =

   1   3
  -2   7

B = load('A.asc')            % or load data from file A.asc into a variable B
B =

   1   3
  -2   7


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% How to solve a linear algebra problem in matlab
%
% two apples, three lemons, and one pear cost $3.30
% one apple, four lemons, and seven pears cost $8.25
% five apples, two lemons, and one pear cost $5.25
%
%  what do apples, lemons, and pears cost, each?

% Translate this into a system of three equations
%
% 2x + 3y + 1z = 3.45
% 1x + 4y + 7z = 8.25
% 5x + 2y + 1z = 5.25

% Rewrite that in matrix-vector form
% [ 2 3 1 ]  [x]   [3.30]
% [ 1 4 7 ]  [y] = [8.25]
% [ 5 2 1 ]  [z]   [5.25]
%   
%     A       x  = b

% Now type A and b into Matlab
A = [ 2 3 1 ; 1 4 7 ; 5 2 1]
A =

   2   3   1
   1   4   7
   5   2   1

b = [3.30 ; 8.25 ; 5.25]
b =
   3.3000
   8.2500
   5.2500

% Use Matlab's backslash operator to solve Ax=b for x
x = A\b
x =
   0.75000
   0.30000
   0.90000

% Thus apples cost $0.75, lemons $0.30, and pears $0.90.
% Verify that Ax=b

A*x
ans =
   3.3000
   8.2500
   5.2500

A*x-b
ans =

   0
   0
   0

% yep!
gibson/teaching/fall-2013/math445/lecture3.txt · Last modified: 2013/09/04 19:17 by gibson