0% found this document useful (0 votes)
3 views22 pages

NC Lect Symbolic 15

The document provides an overview of symbolic calculations in MATLAB, including defining symbolic variables, formatting expressions, and using commands like expand, collect, factor, simplify, and solve. It also covers numerical evaluation of symbolic expressions and basic calculus operations such as limits, derivatives, and integrals. Additionally, it explains the creation and execution of script and function M-files in MATLAB.

Uploaded by

rk0867799
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

NC Lect Symbolic 15

The document provides an overview of symbolic calculations in MATLAB, including defining symbolic variables, formatting expressions, and using commands like expand, collect, factor, simplify, and solve. It also covers numerical evaluation of symbolic expressions and basic calculus operations such as limits, derivatives, and integrals. Additionally, it explains the creation and execution of script and function M-files in MATLAB.

Uploaded by

rk0867799
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Numerical Computing

Symbolic Calculations

Dr. Shahid Rahman


Symbolic Calculations in MATLAB

Defining Symbolic Variables and Expressions

Symbolic variables can be defined in MATLAB using the syms command.


The symbolic variables we have defined appear in the Workspace window.

Example: Suppose we want to symbolically dene the quadratic


equation
y = ax2 + bx + c.

>> syms a b c x
>> y=a*x^2+b*x+c;

Symbolic variables can be cleared using the clear command. In


particular, if x is defined as a symbolic variable, then the command clear
x will delete the variable x. To clear all variables from the Workspace
window at once, enter the command clear.
Symbolic Calculations in MATLAB

ormatting Symbolic Expressions


There are a variety of ways to format symbolic expressions in MATLAB.

The Expand Command

The expand command carries out products by distributing. It can also


be used to expand trigonometric, exponential, and logarithmic
expressions.

Example: Expand the expression Example: Expand the expression


y = (x + 2)(x - 4). z = ex+y.
>> syms x >> syms x y
>> y=(x+2)*(x-4); >> z=exp (x+y);
>> expand(y) >> expand(z)
Example: Expand the expression
f(x; y) = sin(x + y).

>> syms x y
>> f=sin(x+y);
>> expand(f)
Symbolic Calculations in MATLAB

The Collect Command


The collect command groups all terms that have a variable to the same
power.

Example: Suppose we want to group the equation

y = x(x2 +sin x)(x3 +ex) by powers of x.

>> syms x
>> y= x* (x^2+ sin (x)) * (x^3+ exp (x) );
>> collect (y)

Example: Suppose we want to group the


expression

x2y + xy - x2 - 2x by powers of x.

>> syms x y
>> collect (x^2y + x*y - x^2 - 2* x, x)
Symbolic Calculations in MATLAB

The Factor Command

The factor command is used to factor polynomials and other


expressions.

Example: Suppose we want to factor the expression

(x4 - 1) = (x2 + 1)(x + 1)(x - 1):

>> syms x
>> factor(x^4-1)
The Simplify Command Example: Simplify the expression
y = x4 - 81 / x2 -9
The simplify command is used to .
rewrite a symbolic expression with >> syms x
the least number of characters. >> y= (x^4-81) / (x^2-9);
>> simplify(y)
Symbolic Calculations in MATLAB

>> syms x y c
>> f=exp (c*log (sqrt (x^2+
y^2) ) );
>> simplify(f)
Symbolic Calculations in MATLAB

Solving Algebraic Equations


To solve an algebraic equation in MATLAB, we use the solve command.
Symbolic Calculations in MATLAB

Solving Algebraic Equations

Example: Solve the equation x2 - 6x -12 = 0.

>> solve ('x^2-6*x-12')


17
The solve command can also be used to solve systems of equations.
Symbolic Calculations in MATLAB

Numerical Evaluation of Symbolic Expressions


In some cases, it is useful to evaluate a symbolic expression as a
numerical value.
The Eval and Double Commands

To express a symbolic expression as a numerical value, we use the


eval or double command.
Example: Solve the equation x3 + 2x - 1 = 0 and evaluate the result
numerically.
>> s=solve('x^3+2*x-1')
>> eval(s)
>> double(s)

The Subs Command Example: Suppose we want to


symbolically dene the function f(t) = eat
To substitute a particular and evaluate this
value for a symbolic function for t = 0; 1; 2; …
variable into a symbolic >> syms a t Multiple substitutions
expression, we use the >> f=exp(a*t); can be made at the
subs command. >> subs(f,t,0) same time.
>> subs(f,t,1)
Symbolic Calculations in MATLAB

Numerical Evaluation of Symbolic Expressions

Example: Suppose we want to symbolically


dene the function
f(x; y) = sin(x)+cos(y) and

evaluate this function for x = 1 and y = 2.

>> syms x y
>> f=sin (x) + cos (y);
>> subs ( f, fx, yg, f1, 2g)
19
Basic Calculus in MATLAB

Limits
Limits can be calculated in MATLAB by using the limit command. To
compute limits of the form f(x), we use the syntax limit(f,a).

To compute one-sided limits, we use the


syntax limit(f,x,a,'left') or
limit(f,x,a,'right').
Basic Calculus in MATLAB
Basic Calculus in MATLAB

Derivatives
Derivatives can be computed using the diff command. To find the
derivative of a function of one variable f(x), we use the syntax diff (f).
Basic Calculus in MATLAB
Basic Calculus in MATLAB

Integration
Integrals can be computed using the int command. To evaluate indefinite
integrals of the form
Basic Calculus in MATLAB
Basic Calculus in MATLAB

Sums and Products


Sums can be computed using the sum command.
Basic Calculus in MATLAB

M-Files Overviews

Script M-Files

A script M-le is a text le containing a list of MATLAB commands. To


create an M-file, simply click the \New Script" icon in the upper left
corner of the MATLAB window. A new window will appear with MATLAB's
default editor.
Let us write a script M-file to plot the function f(x) = sin(x) on the
interval
[-2pi; 2pi]. In the editor window, enter the following commands:
x=-2*pi:0.01:2*pi; To save this le, click the \Save" icon in
f=sin(x); the upper left corner of the MATLAB
plot(x,f) window and
choose \Save As". Save your M-le as
SinePlot.m. To run this script M-le, click
the \Run“ icon (looks like a green arrow)
at the top of the MATLAB window.
MATLAB goes through
your M-le line by line and executes each
command.
Basic Calculus in MATLAB

Function M-Files
A function M-le is used to perform operations with a function. This
typically involves inputting some variable or variables which are
processed by the M-le and some output is returned. As before, click the
\New Script" icon in the upper left corner of the MATLAB window. A new
window will appear with MATLAB's default editor.
Basic Calculus in MATLAB

Every function M-le begins with the function command, and the input is
placed in paren-theses after the name of the function M-le. To save this le,
click the \Save" icon in the upper left corner of the MATLAB window and
choose \Save As". Save your M-file as CosineDiff.m. For every function M-
file, the file name must be the same as the name of the function. To run
this function M-file, enter the following command in the command window:
>> CosineDiff(0)

The function M-file takes the input n = 0, computes the derivative of


cos(x) at x = 0, and returns the answer in the command window. Function
M-files can also take several inputs at once using the syntax function
Name(x,y,z,...).

function CosineInt(a,b)
syms x
f=cos(x);
Int (f,a,b)
A switch statement takes a variable, say x, and performs different
operations depending on the value of that variable.

Consider the following example:


switch x
case 1 Amazing Switch Statement
y=1;
case 3
y=2; If x = 1, then the variable y is
case 5 assigned the value 1. Similarly, if x =
y=3; 3, then y = 2 and if
end x = 5, then y = 2.
Thanks..!
Any Questions???

You might also like