Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Computer Programming

(CSC209)

Lecture (8)
MATLAB Applications: Simultaneous
Equations, Diff and Integration
Dr.Omar Almutairi
Symbolic Verification
syms shortcut for creating symbolic variables and functions
Example:
>> syms x >> syms f(x, y)
f = x^3+5 >> f(x, y) = x + 2*y
f= f(x, y) =
x^3+5 x + 2*y

>> syms x
>> F(2)
>> F(x)= [x x^2; x^3 x^4]
ans =
F(x) =
[ 2, 4]
[ x, x^2]
[ 8, 16]
[ x^3, x^4]

2
Solving Quadratic Equations
The following example solves the quadratic
equation >> eq = 'x^2 -7*x + 12 = 0';
(1) x2 -7x +12 = 0
s = solve(eq)
>> s
s = roots([1, -7, 12]) 3
s 4
3
4
solve('(x-3)^2*(x-7)=0')
>> ans
(2) (x-3)2(x-7) = 0 3
3
7
3
Solving 2x2 Systems of Equations
−1
• Can use solution x = a b from the single equation to
solve.
2 x1 + 5 x2 = 11 2 5   x1   11 
For example 3x1 − 2 x2 = −12
In matrix form 3 − 2  x  = − 12
  2   
A X = B
• Solution Using MATLAB script
A = [2 5; 3 -2];
b = [11; -12]; X =A-1B
x = inv(A) * b
x =
-2.0000
3.0000

4
Solving 3x3 Systems of Equations
Solve the system of simultaneous equations given by Equations

A = [4 -2 1; 1 1 5; -2 3 -1];
b = [7;10;2];
x = inv(A)*b
x =
3.0244
2.9512
0.80488
3 Simultaneous Equation
Use MATLAB to solve the following set of equations
(1) (2)

x = −2, y = 2

(3) (4)

3 5 -2 6
Expand or Simplify
Why simplify: Working with simpler models result in faster and
more reliable computation than higher order model.
Expand: expand Mathematical expression

❖ expand((x+1)^3) returns x^3+3*x^2+3*x+1


❖ expand(sin(x+y)) returns sin(x)*cos(y)+cos(x)*sin(y)
❖ expand(exp(x+y)) returns exp(x)*exp(y)
❖ expand((exp(x+y)+1)^2)
returns 2*exp(x)*exp(y) + exp(2*x)*exp(2*y) + 1

7
Differential Equation
Example:
syms x y
syms x f = x^2*y^2
f = x^3+5 n = diff(f) % 1st ODE(x)
n = diff(f) % 1st ODE
c = diff(f,y) % 1st ODE(y)
c = diff(f,2) % 2nd ODE
k = diff(f,y,2) % 2nd ODE(y)
Solution:
f=
f= x^2 * y^2
x^3 + 5 n=
n= 2*x*y^2
3*x^2 c=
c= 2*x^2*y
6*x k=
2*x^2 8
Partial Diff Equation
Partial Diff equation: diff for x and y in the same equation

syms x y
f = x^2*y^2
n = diff(f,x) % 1st PDE(x)
c = diff(n,y) % 1st PDE(y)/ 2nd ODE

f=
x^2 * y^2
n=
2*x*y^2
c=
4*x*y
9
Integration Equation
syms x f=
f = x^2 x^2
n = int(f) % integration of f for x
n=
x^3/3
syms x
f=
f = x^2
x^2
n = int(f,x,0,1)
% limited integration of f for x (0 to 1) n=
% substitute 1 and 0 and get the different 1/3
between them

syms x y
f = x^2+y^2; n=
n = int(f,x)%integration of f
x^3/3 + x*y^2
for x n=
n = int(n,x)% 2nd integration (x^2*(x^2 + 6*y^2))/12
Substitute after the integration
syms x y n=
f = x^2+y^2; x^3/3 + x*y^2
n = int(f,x)% integration of
d=
f for x
d = int(n,x)% 2nd integration (x^2*(x^2 + 6*y^2))/12
on x

K = subs(d,x,1)
Substitute the value of x = 1
y^2/2 + 1/12

C = subs(K,x,1)
Substitute the value of y = 2
25/12

Substitute the value of x and y C = subs(d,{x,y},{1,2})


= (1,2) accordingly 25/12 11
Substitute Variables
n=
(x^3*y^2)/3
d=
(x^4*y^2)/12
Replace x by a
syms a
P = subs(d,x,a)
(a^4*y^2)/12
Replace x by 1:3 (3 equ)

P = subs(d, x, 1:3)
[ y^2/2 + 1/12, 2*y^2 + 4/3, (9*y^2)/2 + 27/4]
12
Assignment
(1)

(2)

Please Create one script file for both them.


Then upload it to Blackboard under
Assignment 2

Please Upload your script file you did at


the end of lec 6 under Assignment 1

Please use % for denoting questions and


descriptions
13

You might also like