0% found this document useful (0 votes)
289 views

Matlab Activity 5.1

This document provides an overview of symbolic math tools in MATLAB. It discusses how to create symbolic variables, manipulate symbolic expressions, solve equations symbolically, perform substitutions on symbolic expressions, and take derivatives and integrals of symbolic expressions. The document includes examples of the syntax and output for each task. It then provides two tasks for the student to complete - the first involving derivatives and the second involving integrals of symbolic expressions. The student is asked to write the MATLAB syntax and output for each part.

Uploaded by

roseleen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
289 views

Matlab Activity 5.1

This document provides an overview of symbolic math tools in MATLAB. It discusses how to create symbolic variables, manipulate symbolic expressions, solve equations symbolically, perform substitutions on symbolic expressions, and take derivatives and integrals of symbolic expressions. The document includes examples of the syntax and output for each task. It then provides two tasks for the student to complete - the first involving derivatives and the second involving integrals of symbolic expressions. The student is asked to write the MATLAB syntax and output for each part.

Uploaded by

roseleen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MATH 012/012B NUMERICAL METHODS AND ANALYSIS

MATLAB ACTIVITY 5.1

SYMBOLIC MATH IN MATLAB

NAME :
SCORE:

SECTION: ACTIVITY NO:

INSTRUCTOR: DATE PERFORMED/SUBMITTED

CREATING SYMBOLIC VARIABLES


Before we can solve any equations, we need to create symbolic variables. Simple symbolic variables can be created
as follows. For example, to create the symbolic variable x, type
>> syms x

More complicated variables can be created by using existing symbolic variables, as in the expression
>> y=2*(x+3)^2/(x^2+6*x+9)

The syms command is particularly convenient, because it can be used to create multiple symbolic variables at the
same time, as with the command
>> syms Q R T k

These variables could be combined mathematically to create another symbolic variable, m


>> m=k*exp(-Q/(R*T))

Notice that in both examples we used the standard algebraic operators, not the array operators such as .* or .^.

MANIPULATING SYMBOLIC EXPRESSIONS AND SYMBOLIC EXPRESSIONS

Extracting Numerators and Denominators

The numden function extracts the numerator and denominator from an expression. For example, if you’ve
defines y as
>> y=2*(x-3)^2/(x^2+6*x+9)

then you can extract the numerator and denominator with


>> [n,d] = numden(y)
num = 2*(x-3)^2

den = x^2+6*x+9
Expanding Expressions, Factoring Expressions, and Collecting Terms

We can use the expressions we have defined earlier to demonstrate the use of the expand, factor, and
collect function. Thus,

>> expand(n)
returns
ans =
2*x^2-12*x+18

and

>> factor(d)
returns
ans =
[x+3, x+3]

The collect function collects like terms and is similar to the expand function:

>> collect(n)
returns
ans =
2*x^2-12*x+18

Simplification Functions

We can think of the expand, factor, and collect functions as ways to simplify an expression or
equation. However, what constitutes a “simple” equation is not always obvious. The simplify function
simplifies each part of an expression or equation. For example, assume the following,

>> syms a
>> z=3*a-(a+3)*(a-3)^2
>> simplify(z)
returns
ans =
3*a-(a-3)^2*(a+3)

Notice that we obtain the same expression. This means that the given is already in simplified form. Now let
us consider another example,

>> syms x
>> w=x^3-1==(x-3)*(x+3)
>> simplify(w)
returns
ans =
x^3 + 8 = x^2

Remark:
A shortcut to create a symbolic polynomial is the poly2sym function. This function requires a vector as
input and creates a polynomial, using the vector for the coefficients of each term of the polynomial.
>> a=[1, 3. 2]
a=
1 3 2

>>b= poly2sym(a)
returns
b=
x^2+3*x+2

Similarly, the sym2poly function converts a polynomial into a vector of coefficient values:
c= sym2poly(b)
returns
c =
1 3 2

The Solve Function

When used with an expression, the solve function sets the expression equal to zero and solves for the
roots. For example,

>> syms x
>> E1 = x-3
then
solve(E1)
returns
ans =
3

The solve function can also be used wither with an expression name or by creating a symbolic expression
directly in the solve function. Thus,

>>solve(x^2-9)
returns
ans =
-3
3

You can readily solve symbolic expressions with more than one variable. For example, for the quadratic
expression ax2 +bx + c,

>> solve(a*x^2 +b*x + c)


returns
ans =
-(b + (b^2-4*a*c)^(1/2))/(2*a)
-(b - (b^2-4*a*c)^(1/2))/(2*a)

Matlab preferentially solves for x. If there is no x in the expression, Matlab finds the variable closest to x. If
you want to specify the variable to solve for, just include it in the second field. For instance, to solve the
quadratic expression for a, the command
>> solve(a*x^2 +b*x + c , a)
returns
Ans =
-(c+b*x)/x^2

To solve an expression set equal to something besides zero, you must use one of the two approaches. If the
equation is simple, you can transform it into an expression by subtracting the right-hand side from the left-
hand side. For example
5x2 + 6x +3 = 10

could be reformulated as

5x2 + 6x - 7 = 0
then
>> solve(5*x^2 + 6*x – 7)
ans =
-2(2*11^(1/2))/5 – 3/5
2(2*11^(1/2))/5 – 3/5

The solve function is particularly useful with symbolic expressions having multiple variables. For
example,
>> syms P P0 r t
>> E2=P == P0*exp(r*t)
>> solve(E2,t)

returns
ans =
log(P/P0)/r

Solving Systems of Equations

Not only can the solve function solve single equations or expressions for any of the included variables, it
can also solve systems of equations. Take, for example, these three symbolic equations
>> syms x y z
>> one = 3*x + 2*y – z == 10;
>> two = -x + 3*y + 2*z == 5;
>> three = x – y – z == -1;

To solve for the three embedded variables x, y, and z, simply list all three equations in the solve function:
>> [x, y, z] = solve(one,two,three)
x =
-2
y =
5
z =
-6

Substitution

Particularly for engineers or scientists, once we have a symbolic expression, we often want to substitute values
into it. Consider the quadratic equation:
>> syms x y a b c
>> E3 = a*x^2 + b*x + c

Let us say we want to evaluate E3 using the substitution x=y, we use the syntax
>> subs(E3, x , y)

which returns
ans =
a*y^2 + b*y + c

We can make multiple substitutions by listing the variables inside curly brackets, defining a cell array:
>> subs(E3,{a,b,c,x},{1,2,3,4})

This means that we will substitute to ax 2+bx+c the values a=1, b=2, c=3, and x=4. This will return
ans =
27

TASK 1

Create the following symbolic variables using the syms command x, a , b, c, d


Create the following expressions

ex1 = x2-1
ex2 = (x+1)2

ex3 = ax2 – 1

ex4 = ax2 + bx + c
ex5 = ax3 + bx2 + cx + d

Create the following equations


eq1 = x2-1 = 1

eq2 = (x+1)2 = 0
eq3 = ax2 = 1

eq4 = ax2 + bx + c = 0
eq5 = ax3 + bx2 + cx + d = 0

Perform the following and write the proper syntax and corresponding output in Matlab.

Task Syntax Output


a) Multiply ex1 by ex2, and name
the result y1.

b) Divide ex1 by ex2, and name the


result y2.

c) Extract the numerator and


denominator from y1 and y2.

d) Use the factor, expand and


simplify functions on y1 and y2.

e) Solve for x in ex3 and eq3

f) Solve for a in ex4 and eq4

g) Solve the system


5x+6y-3z=10
3x-3y+2z=14
2x-4y-12z=24

h) Evaluate ex5 using a=3, b=4,


c=5, d=6, x=-2
DIFFERENTIATION

The acceleration is the first derivative of the velocity and the second derivative of the position. Matlab offers
several slightly different techniques to find both first derivatives and nth derivatives.

If we have a more complicated equation with multiple variables, such as


>> syms x t z
>> y = x^2 + t – 3*z^3

Matlab will calculate the derivative with respect to x, the default variable:
>> diff(y)

returns
ans =
2*x

Our result is the rate of change of y as x changes (if we keep all the other variables constant). This is usually
depicted as the partial derivative. If we want to see how y changes with respect to another variable, such as t, we
must specify in the diff function:
>> diff(y,t)
ans =
1

Similary, to see how y changes with z when everything else is kept constant, we use
>> diff(y, z)
ans =
-9*z^2

To find higher order derivatives, we can specify the order of differentiation as a separate field inside the diff
function.
>> diff(y,2)
returns
ans =
2

This is the second derivative of y with respect to x.


Also, if we want to get the third derivative of y with respect to z, then the command is
>> diff(y, z, 3)

returns
ans =
-18
INTEGRATION

Integration can be thought as the opposite of differentiation and is even sometimes called the antiderivative. It is
commonly visualized as the area under a curve.

The int function uses x as the default variable. For example, if we define a function with two variables, the int
function will find the integral with respect to x of the variable closest to x:
>> syms x t
>> y = x^3 + sin(t)
>> int(y)

returns
ans =
1/4*x^4 + sin(t)*x

If we want to take the integral with respect to a user-defined variable, that variable needs to be specified in the
second field of the int function:
>> int(y, t)
ans =
x^3*t – cos(t)

To find the definite integral, we need to specify the limits of integration. Consider this expression:
y = x^2

If we don’t specify the limits of integration, we get


>> int(y)
ans =
1/3*x^3

We could evaluate this from 2 to 3 using the int function by specifying the bounds inside:
>> int(y,2,3)
ans =
19/3

The limits of integration can be numeric, or they can be symbolic variables:


>> syms x z
>> y = sin(x) + cos(z)
>> int(y, z, b , c)
ans =
sin(c)– sin(b) - sin(x)*(b-c )
TASK 2

1. Find the first partial derivative with respect to x of the following. Write the syntax and output on the table
below.

Expression First Partial Derivative with Second Partial Derivative with


respect to x (Syntax and output) respect to x (Syntax and output)
a) ax2 + bx + c

b) x0.50 – 3y

c) tan(x+y)

d) 3x + 4y – 3xy

2. Find the first partial derivative with respect to y of the following. Write the syntax and output on the table
below.

Expression First Partial Derivative with Second Partial Derivative with


respect to y (Syntax and output) respect to y (Syntax and output)
e) y2 - 1

f) 2y + 3x2

g) ay + bx + cz
3. Integrate the following expressions with respect to x. Write the syntax and output on the table below.

Expression Integral with respect to x (Syntax Integral with respect to x


and output) evaluated from 0 to 5 (Syntax
and output)
h) ax2 + bx + c

i) x0.50 – 3y

j) tan(x+y)

k) 3x + 4y – 3xy

You might also like