HOW DO I DO THAT IN MATLAB SERIES?

In this series, I am answering questions that students have asked me about MATLAB. Most of the questions relate to a mathematical procedure.

Contents

TOPIC

How do I solve a set of simultaneous linear equations?

SUMMARY

% Language : Matlab 2008a;
% Authors : Autar Kaw;
% Mfile available at
% http://numericalmethods.eng.usf.edu/blog/sle.m;
% Last Revised : August 12, 2009;
% Abstract: This program shows you how to solve a set of simultaneous linear
% equations?
%           .
clc
clear all
clf

INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you how to solve a')
disp('   set of simultaneous linear equations')
disp(' ')
disp('AUTHOR')
disp('   Autar K Kaw of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp('   http://numericalmethods.eng.usf.edu/blog/sle.m')
disp(' ')
disp('LAST REVISED')
disp('   August 12, 2009')
disp(' ')
ABSTRACT
   This program shows you how to solve a
   set of simultaneous linear equations
 
AUTHOR
   Autar K Kaw of http://autarkaw.wordpress.com
 
MFILE SOURCE
   http://numericalmethods.eng.usf.edu/blog/sle.m
 
LAST REVISED
   August 12, 2009
 

INPUTS

Enter the coefficient matrix of the equation [A][X]=[C]

A=[12  23  39; 13  17  19; 21  23  29];
% Enter the right hand side vector
C=[29;   37;  59];

DISPLAYING INPUTS

disp('  ')
disp('INPUTS')
disp('________________________')
disp('Coefficient Matrix')
disp('________________________')
dataval=[A];
disp(dataval)
disp('________________________')
disp('Right hand side vector')
disp('________________________')
dataval=[C];
disp(dataval)
disp('________________________')
  
INPUTS
________________________
Coefficient Matrix
________________________
    12    23    39
    13    17    19
    21    23    29

________________________
Right hand side vector
________________________
    29
    37
    59

________________________

THE CODE

The solution

X=A\C;

DISPLAYING OUTPUTS

disp('  ')
disp('OUTPUTS')
disp('________________________')
disp('Solution Vector')
disp('________________________')
dataval=[X];
disp(dataval)
disp('________________________')
  
OUTPUTS
________________________
Solution Vector
________________________
    2.9520
    0.3026
   -0.3432

________________________