The MATLAB Programming Language
Lecture 3
Dr. Eng. Samer Mohamed Elabd
Quiz 1
20 mln
Systems of linear equations
Systems of linear equations are very important
in engineering and scientific analysis.
A simple example is finding the solution of two
simultaneous equations, e.g.
x + 2y = 4,
2x − y = 3.
Systems of linear equations
Approach 1: This approach is the matrix
method approach.
Type the following commands (exactly as they
are):
>> a = [1 2; 2 -1]; <Enter>
>> b = [4; 3]; <Enter>
>> X = a\b <Enter>
x=
2
y=
1
Systems of linear equations
Approach 2: This approach uses the built-in solve
function.
>> [x,y] = solve(’x+2*y=4’,’2*x-y=3’) <Enter>
x=
2
y=
1
Systems of linear equations
Check of results: After executing either (a) or (b)
above type the following commands (exactly as
they are):
>> x + 2*y % should give ans = 4 <Enter>
ans =
4
>> 2*x - y % should give ans = 3 <Enter>
ans =
3
The % symbol is a flag that indicates all information to the right is not
part of the command.
Systems of linear equations
>> matrix = [1 2 3; 4 5 6]
To Edit data:
Assign a value to a certain position in a
matrix
Example: matrix(2,3) = 100
To Delete a row :
Matrix(9,:) = [];
Example: delete Column 4 of your matrix
Systems of linear equations
x1 − 2x2 + 3x3 =4
2x1 − 5x2 + 12x3 = 15
2x2 − 10x3 = −10,
Ax = b
Systems of linear equations
Ax = b
In Matlab the standard way to solve a system Ax
= b is by the command:
>> x = inv(A)*b
or
>> x = A\b
We can do the above computations as follows:
>> A = [1 -2 3 ; 2 -5 12 ; 0 2 -10]
>> b = [4 15 -10]'
>> x = A\b
Systems of linear equations
5 ton 4 7 5
Sheet
Q1
1 1 2 3
F8
2 7 ton
F9 F10
In the problem above
1. How many equations will be generated?
2. In the problem above, how many unknowns are there?
3. In the problem above, each element is 5 m long. Construct the
matrix you would solve to find the forces in the elements and the
reactions. Use the element and node numbering shown.
Systems of linear equations
Sheet
Q2 W t/m
A B
x
Write a Matlab function to calculate and plot the
bending moment at point c for different distance x
from the left support.
Curve Fitting
In each of the following problems, determine
the best function y(x) (linear, exponential, or
power function) to describe the data.
Plot the function on the same plot with the
data. Label and format the plots appropriately.
x 25 30 35 40 45
a)
y 5 260 480 745 1100
b)
x 2.5 3 3.5 4 4.5 5 5.5 6 7 8 9 10
y 1500 1220 1050 915 810 745 690 620 520 480 410 390
Curve Fitting
The population data for a certain country are as
follows:
Year 2004 2005 2006 2007 2008 2009
Population
10 10.9 11.7 12.6 13.8 14.9
(millions)
Obtain a function that describes these data.
Plot the function and the data on the same
plot.
Estimate when the population will be double its
2004 size.
Curve Fitting
Use fit function of cftool
>> cftool
>> [cfun,gof,output] = fit(x',y', ‘exp1')
Try typing the following in matlab
>>f(x) = x^2+3*y;
Undefined function or variable 'x'.
Try the following
>> syms x y
>> f(x,y) = x^2+3*y;
Now try to evaluate the function f at x = 1 and y =2
>> f(1,2)
ans =
7
Symbolic matrix
Try the following
>> clear; x=sym(‘x’); y=sym(‘y’);
>> f(x,y) = x^2+3*y;
What is the difference
>> s=f(1,2)
s=
7
The result s is a symbolic matrix
To Convert symbolic matrix to MATLAB numeric form
>> s=double(s)