Matlab Activity 5.1
Matlab Activity 5.1
NAME :
SCORE:
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
Notice that in both examples we used the standard algebraic operators, not the array operators such as .* or .^.
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)
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
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,
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
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
ex1 = x2-1
ex2 = (x+1)2
ex3 = ax2 – 1
ex4 = ax2 + bx + c
ex5 = ax3 + bx2 + cx + d
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.
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.
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
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
We could evaluate this from 2 to 3 using the int function by specifying the bounds inside:
>> int(y,2,3)
ans =
19/3
1. Find the first partial derivative with respect to x of the following. Write the syntax and output on the table
below.
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.
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.
i) x0.50 – 3y
j) tan(x+y)
k) 3x + 4y – 3xy