CHE334 PROCESS MODELLING
AND SIMULATION
BY
PROF. V.E EFEOVBOKHAN
Covenant University
Lecture 6
• More of MatLab functions
• Rem(x,y) means the remainder of x
divided by y.
• Example
• >> rem(10,3)
• ans = 1
• >> rem(100,3);x
• Undefined function or variable 'x'.
• >> rem(100,3)
• ans = 1
• You can also assign a variable to the
evaluation.
• x=rem(100,3)
• x= 1
• To find the factors of a number, use
‘factor’ e.g.
• factor(36)
• ans = 2 2 3 3
• >> factor(100)
• ans = 2 2 5 5
• > x = factor(24)
• x= 2 2 2 3
• Format: The Way in Which Numbers
• Appear
• Consider the following codes
• s = [1/2 1/3 pi sqrt(2)];
• format short; s
• format long; s
• format rat; s
• format ; s
• >> s = [1/2 1/3 pi sqrt(2)];
• >> format long, s
• s = 0.500000000000000,
0.333333333333333
3.141592653589793
1.414213562373095
• >> format rat, s
• s= 1/2 1/3 355/113
1393/985
• >> format; s
• s = 0.5000 0.3333 3.1416 1.4142
• Some MatLab specific command.
3 2
• Evaluate the cubic y =𝑥 + 3𝑥 − 𝑥 −
1 at the point x = [1 2 3 4 5 6].
• We find the solution to this code as a
commented code.
• >> %firstly set up the points at which
the polynomial will be evaluated
• >> x = 1:6;
• >> % enter the coefficients of the cubic
equation and note that
• >> % these are entered starting with
the coefficients of the
• >> % of the highest power first
• >> c = [1 3 -1 -1];
• >> % now perform the evalution using
polyval
• >> y = polyval(c,x)
• y = 2 17 50 107 194 317
• It is always a good practice to provide
brief, but meaningful, comments at
important points within your code
• Plot the polynomial y = 𝑥 4 + 𝑥 2 − 1
between x = −2 and x = 2 (using fifty
points).
• >> x = linspace(-2,2,50);
• >> c = [1 0 1 0 -1];
• >> y = polyval(c,x);
• >> plot(x,y)
• Find the roots of the polynomial
𝑦 = 𝑥 3 − 3𝑥 2 + 2𝑥 using the
command roots
• c = [1 -3 2 0];
• r = roots(c)
• r =0
• 2
• 1
• The solve function is used for solving
algebraic equations.
• Solve for x in the equation x-5 =0
• y = solve(‘x-5=0’)
• MATLAB will execute some statements
and return
• y=5
• The solve function takes the equation
enclosed in quotes as an argument
• If the equation involves multiple
symbols, then MATLAB by default
assumes that you are solving for
• x, except you specify the one you are
solving.
• Example
• solve(equation, variable)
• solve for v in the equation v – u – 3t2 =
0,
• >> >> solve('v-u-3*t^2=0','v')
• ans = 3*t^2 + u
• Solve for t.
• >> solve('v-u-3*t^2=0','t')
• -(3^(1/2)*(v - u)^(1/2))/3
• (3^(1/2)*(v - u)^(1/2))/3
• We can still solve the equations in
octave using the roots function.
• >> y= roots([1,-5])
• y=
• 5
• >> c =[1 -5];
• >> roots(c)
• ans =
• 5
• solve the fourth order equation x4 − 7x3
+ 3x2 − 5x + 9 = 0.
• >> c=[1 -7 3 -5 9];
• >> s=roots(c)
• s=
• 6.6304 + 0.0000i
• 1.0598 + 0.0000i
• -0.3451 + 1.0778i
• -0.3451 - 1.0778i
• >> eq = 'x^4 - 7*x^3 + 3*x^2 -5*x +9 =
0';
• >> r = solve(eq)
• r=
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 1)
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 2)
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 3)
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 4)
• Solve the following system of equations
• x + 2y + 3z = 1
• 3x + 3y + 4z = 1
• 2x + 3y + 3z = 2
• Solution
• >> A = [1 2 3;3 3 4;2 3 3];
• >> b = [1;1;2];
• >> X = A\b
• X = -0.5000
• 1.5000
• -0.5000
Solve the equations
• 2x + 3y = 7,
• x − y = 1,
• and
• 2x + 3y = −2,
• x − y = 8.
The equations can be written as
2 3 𝑥 7 −2
=
1 −1 𝑦 1 8
• >> A = [2 3;1 -1];
• >> b = [7 -2;1 8];
• >> X = A\b
• X=
• 2.0000 4.4000
• 1.0000 -3.6000
• The Solution to the 1st equation is (2, 1)
and the 2nd is (4.4, -3.6)
Solve the following simultaneous equations
(a) y = x+1 and x+y =3
(b) y = x+1 and x+y = 5
(c) y = x2 and y = x+2
(d) Y =x2 -3x + 4 and y – x = 1
>> x = -4:0.1:4;
>> y1 = x + 1;
>> y2 = 3-x;
>> plot(x,y1,x,y2)
>> xlabel('-4\leqx\leq4')
>> ylabel('Y')
>> grid
>> title('graphical solution of simultaneous
equation')
>> legend('y1=x+1','y2=x-3')
Under edit
Click on figure properties
Click on the each of the lines and effect what ever
changes you desire
Solve the quadratic equations
(a) X2 - 6x +9=0