Saturday, February 14, 2015

MATLAB TuTer 4: Algebraic calculations using MATLAB



Algebraic calculations in MATLAB
          
There is a simple way to define and evaluate a function, which is using the symbol "@" or called function handle, as following example:

>> Sine = @sin; % define a function named "Sine" to apply sinusoidal. 

>> Sine(pi/2) % apply Sine by the value "pi/2"

ans =     1

·         define a function called "function1", where (x,y) are the function independent and "x^2+(x+y)-2*y" is the function expression:

>> function1 = @(x,y) x^2+(x+y)-2*y

function1 =    @(x,y)x^2+(x+y)-2*y

>> function1(2,3) % apply function1 in (2,3) as (x,y).

ans =     3

·         define function called "MainFunction"  has three independents x, y, and another function "f", where "f" can be any other predefined function:

>> MainFunction = @(x,y,f) f(x^2,y^2)

MainFunction =    @(x,y,f)f(x^2,y^2)

>> MainFunction(2,3,function1)

ans =    11

Solving linear algebraic equations:

We can solve all linear or polynomial equations of algebra using MATLAB, if we have the equations:
·                    Equation1 (x – 4 = 0)
·                    Equation2 (x2 – 4 = 0)
·                    Equation3 (x2 + 4 = 0)
·                    Equation4 (x2 – 3x + 9 = 0)
·                    Equation5 (x5 – 3x4 + x – 12 = 0)
·                    Equation6 (x3 – 2x2 = x – 2)
For these types of equations which have the value zero in the right side or it can be, we better use the matrix method to input or enter our equation coefficients in the particular way. So, for the previously mentioned equations the proper input way to MATLAB is like:
Mathematically
In MATLAB
Equation1 (x – 4 = 0)

>> Equation1=[1 -4]
Equation2 (x2 – 4 =0)

>> Equation2=[1 0 -4]
Equation3 (x2 + 4 = 0)

>> Equation3=[1 0 4]
Equation4 (x2 – 3x + 9 = 0)
>> Equation4=[1 -3 9]

Equation5 (x5 – 3x4 + x – 12 = 0)

>> Equation5=[1 -3 0 0 1 -12]
Equation6 (x3 – 2x2 = x – 2)

>> Equation6=[1 -2 -1 2]
-         You can use any name to define the vector variable that holds the equation.
you we should enter the equations as a row vectors the values of it elements is the coefficients of the equation independents, and putting zero if there is an independent messing as in the Equations (2, 3, and 5) , and if the right side of equation is not zero then we just change its sign as in Equation6.
After entering the equation then we should calculate the answer which is the roots of equation, MATLAB has a commonly used function for this purpose which is "roots(X)", where X is the equation name entered before. So, for to find the roots of the previously mentioned equations use:
>> roots(Equation1)
ans =
     4
>> roots(Equation2)
ans =
    2.0000
   -2.0000
>> roots(Equation3)
ans =
   0.0000 + 2.0000i
   0.0000 - 2.0000i
-         And so on, as we can see MATLAB does the calculations and gives the answer we need, the number of answer values here is depending on the degree of the equation as we now if the equation of the second degree we should get two values if of the third degree we should get three values and so on. Complete the roots of Equation(4, 5, and 6).

Solving a system of linear algebraic equations:

Example1:
X + 2Y = 4
3X + 4Y = 5
Here we also use the matrix method to enter the equations into MATLAB, but here we use two matrices one for the left side of equations each equation in one row and one for the right side of the equations row for each one also. So, let's define the two matrices for this example and let's name them A and B:
>> A = [1 2; 3 4]
A =
     1     2
     3     4
>> B = [4 ; 5]
B =
     4
     5
Then we have many ways to solve this simple system of linear algebraic equations:
·                    First method: inverse the left side's matrix A and multiply it by the right side's matrix, and we can do this by two ways as below:
>> C = inv(A) * B
C =
   -3.0000
    3.5000
>> C = A^-1 * B
C =
   -3.0000
    3.5000
As we see we get the same answer in the two ways, and hold the answer or the solution of the system in the variable C which result a column vector, element for each independent of X and Y .
·                    Second method: use Gaussian elimination to calculate the solution, that by type the back slash( \ ) symbol on the keyboard between A and B:
>> A\B
ans =
   -3.0000
    3.5000

·                    Third method: use the Built-in MATLAB function for solving linear equations which is "linsolve(A,B)", where A and B are a matrices, so :
>> linsolve (A,B)
ans =
   -3.0000
    3.5000

Example2:
2X – Y + 3Z = 5
4X + 5Z = 12
X + Y + 2Z = -3
Solution:
-         Entering the system of equation in the two matrices:
>> A = [2 -1 3; 4 0 5; 1 1 2]
A =
     2    -1     3
     4     0     5
     1     1     2
>> B = [5 ; 12 ; -3]
B =
     5
    12
    -3
-   Use one of the previous method to find the solution:
>> C = inv(A)*B
C =
   10.0000
   -1.8000
   -5.6000
>> C = A^-1 * B
C =
   10.0000
   -1.8000
   -5.6000
>> C = A\B
C =
   10.0000
   -1.8000
   -5.6000
>> C = linsolve(A,B)
C =
   10.0000
   -1.8000
   -5.6000

·                      Forth method using Row Reduced Echelon Form:

Linear algebra equations can be represented by vectors as mentioned before, so a number of these equation can be set as a matrix, for example:
A + 2B – 3C = 5 , [1 2 -3 5]
2A – B + 4C = 11 ,[2 -1 4 11]
-A + 3B = 5, [-1 3 0 5]
So, these three vectors can be combined into one matrix X, like:
X=[1 2 -3 5; 2 -1 4 11; -1 3 0 5]
X =
     1     2    -3     5
     2    -1     4    11
    -1     3     0     5
The solution of these three equations which is the values of A, B, and C can be get by using the Row Reduced Echelon function "rref (X)", by:
>> rref(X)
Then the solution will be, like:
    1.0000         0         0    3.8286
         0    1.0000         0    2.9429
         0         0    1.0000    1.5714
From the result matrix, we can get the values of A,B, and C, Where: A = 3.8286, B = 2.9429 , C = 1.5714.

Solving algebraic equations symbolically:

Example1: if we have the equation: X + 3 = 2, to solve this equation symbolically first define X as symbolic variable then the function syntax:
solve('equation expression', independent), where:
Equation expression here = X + 3 = 2
The Independent here = X
So, the MATLAB command line will be:
>> syms X
>> solve('X+3=2',X)
 ans =
 -1

Example2:
X2 + 2X -3 = 12
Solution:
>> syms X
>> solve('X^2+2*X-3=12',X)
 ans =
  3
 -5
Example3:
Sin(Y) = 10
Solution:
>> syms Y
>> solve('sin(Y)=10',Y)
ans =
      asin(10)
 pi - asin(10)

Example4:
>> syms Z
>> angle = solve('tan(Z)=1',Z)
angle =
pi/4
>> degrees = double(angle)*180/pi
degrees =
    45

Example4:
X + 2Y = -3Z
Solution:
>> value_of_X = solve('X + 2*Y = -3*Z',X)
value_of_X =
- 2*Y - 3*Z
>> value_of_Y = solve('X + 2*Y = -3*Z',Y)
value_of_Y =
- X/2 - (3*Z)/2
>> value_of_Z = solve('X + 2*Y = -3*Z',Z)
value_of_Z =
- X/3 - (2*Y)/3

Example5:
Sine(X) + ez = 2Y
Solution:
>> for_X = solve('sin(X)+exp(Z)=2*Y',X)
for_X =
 pi + asin(exp(Z) - 2*Y)
     -asin(exp(Z) - 2*Y)
>> for_Y = solve('sin(X)+exp(Z)=2*Y',Y)
for_Y =
exp(Z)/2 + sin(X)/2
>> for_Z = solve('sin(X)+exp(Z)=2*Y',Z)
for_Z =
log(2*Y - sin(X))

Solving a system of equations symbolically:

Here we will use the previous method of "solve" command but with other syntax, for example if we have to find the solution of the two equations:
3X – Y = 2
X + Y = 1
The command line will be :
>> [X Y] = solve('3*X-Y=2','X+Y=1')
 X =
 3/4
  Y =
 1/4
Or we can use also:
>> systen_Equations = solve('3*X-Y=2','X+Y=1')
systen_Equations =
    X: [1x1 sym]
    Y: [1x1 sym]
>> systen_Equations.X
ans =
3/4
 >> systen_Equations.Y
 ans =
 1/4
Example6:
2X – 3cY = 5
cX + 2y = 7
Where c is a constant will not be defined as symbolic.
Solution:
>> syms X Y
>> [X Y] = solve ('2*x-3*c*y=5','c*x+2*y=7')
 X =  (21*c + 10)/(3*c^2 + 4)
 Y = -(5*c - 14)/(3*c^2 + 4)

Defining mathematical functions f(x):

If we have a function like this: F(X) = X2 + 5, Then to define it into MATLAB and use it use the "inline" function of MATLAB by:
"Function_Name=inline('expression','independent1', 'independent2',…)"
So for this example:
>> function1 = inline('X^2+5','X')
function1 =
     Inline function:
     function1(X) = X^2+5
Then use the function name to apply the function with any value of the independent variable, for this example we can use:
>> function1(7)
ans =
    54
>> function1(12.5)
ans =
  161.2500
>> function1(14+2i)
ans =
   1.9700e+02 + 5.6000e+01i

Example:
F(M) = 3M3 + 2M2 + M + 1 , find F(6), F(12.5), F(2i).
Solution:
>> function2 = inline('3*M^3+2*M^2+M+1','M')
function2 =
     Inline function:
     function2(M) = 3*M^3+2*M^2+M+1
>> function2(6)
ans =
   727
>> function2(12.5)
ans =
   6.1854e+03
>> function2(2i)
ans =
  -7.0000 -22.0000i

You can add more independent variables to a function to take their values and apply it to the function, for example if we want to create a function that calculate the Area of rectangle we need two variable the Length "L" and the Width "W", so the function can be like:
>> Area = inline('L*W','L','W')
Area =
     Inline function:
     Area(L,W) = L*W
-   So the function is created and can be used with any values of (L,W), for e.g:
>> Area(5,6)
ans =
    30
>> Area(12,134)
ans =
        1608
Example3:
F(X,Y,Z) = sin(X)+ 3eY – log(Z) , find F(30,-2pi,2.5i)
Solution:
>> syms X Y Z
>> function3 = inline('sin(X)+3*exp(Y)-log(Z)','X','Y','Z')
function3 =
     Inline function:
     function3(X,Y,Z) = sin(X)+3*exp(Y)-log(Z)
>> function3(30,-2*pi,2.5i)
ans =
  -1.8987 - 1.5708i

No comments:

Post a Comment