Matlab Symbolic Math Toolbox 5
Proceedings of symposia in applied mathematics [Book]By Susan G. Williams, American Mathematical Society - American Mathematical Society (2004) - Hardback - 156 pages - ISBN 0821831577
Details
Combining modulation codes and error correcting codes: 13
Complex dynamics and symbolic dynamics: 37
Multidimensional symbolic dynamics: 61
Symbolic dynamics and tilings of Rd: 81
Strong shift equivalence theory: 121
[ Report abuse or wrong photo | Share your Matlab Symbolic Math Toolbox 5 photo ]
Manual
Download
(English)
|
Matlab Symbolic Math Toolbox 5
Video review
Iran Khayyam Part 5 of 8 Intoxicating Rhymes&Sobering Wine A Documentary about Khayyam Persian Poet
User reviews and opinions
| crashkahuna |
8:06am on Wednesday, August 4th, 2010 ![]() |
| Product was purchased for grand daughter and she loves it. Easy To Set Up","Excellent Gameplay","Fun For All Ages","Great Graphics". | |
| bdchambers79 |
4:17am on Tuesday, July 27th, 2010 ![]() |
| ds original-sucked : good lighting options,better touchscreen,more options,more portable. quite old now, pretty rubbish games, younger people find it hard to read the things on screen. | |
| partner |
9:23am on Tuesday, July 20th, 2010 ![]() |
| Able to surf the net with DS Browser A bit pricey it has no pros its so awful it sooooooooooooooooo aful | |
| ceejld |
1:54pm on Wednesday, June 30th, 2010 ![]() |
| Can use both Gameboy and DS game chips! It is for kids. Easy To Set Up","Excellent Gameplay","Fun For All Ages","Great Graphics". | |
| terabiteau |
3:59pm on Sunday, June 13th, 2010 ![]() |
| Great Company The company is very trustworthy. They sent my product out right away. ????? how many hours does this really hold a charge? has any one tryed this with the tap to talk app? | |
| Zed |
9:39pm on Tuesday, May 25th, 2010 ![]() |
| I LOVE IT This system is awesome. It plays all the GameBoy Advance games, as well as the DS games. Can network, etc. My daughters LOVE it. | |
| bdiemer |
11:20am on Tuesday, May 18th, 2010 ![]() |
| I had one of the original Nintendo DS games. This new design is much more streamlined, however. Yeah, the new DSi is out, but heck, can it still play advance games? no. So DS lite is still it. Love it. Dell is great the Metallic Pink looks way better when its in your hand as opposed to online. Super easy to use and rechargable. Just overall fun "toy none | |
| rbcyouthdude |
7:18am on Thursday, May 13th, 2010 ![]() |
| Small, compact design makes it great for traveling. Great portable gaming system! Broke on first day :( | |
| bheck |
5:39pm on Sunday, April 25th, 2010 ![]() |
| A waste of money!! I bought mine to occupy my... compact/nice design The games that are for the ds are terrible ds original-sucked : good lighting options,better touchscreen,more options,more portable. | |
| ekim |
8:13am on Monday, March 22nd, 2010 ![]() |
| The Nintendo DS Lite is the advanced version of the original Nintendo DS. You can play Game Bow Advanced games on it. | |
Comments posted on www.ps2netdrivers.net are solely the views and opinions of the people posting them and do not necessarily reflect the views or opinions of us.
Documents
x2 + x + 2 x2 1
Moreover, normal automatically cancels common factors in the numerator and the denominator:
normal(x^2/(x + y) - y^2/(x + y))
Conversely, partfrac (short for partial fraction) decomposes a rational expression into a sum of rational terms with simple denominators:
partfrac(g, x)
+1 x1 x+1
The functions simplify and Simplify are universal simplifiers and try to find a representation that is as simple as possible:
simplify((exp(x) - 1)/(exp(x/2) + 1))
eYou may control the simplification by supplying simplify or Simplify with additional arguments (see the online documentation for details). The function radsimp simplifies arithmetical expressions containing radicals (roots):
f := sqrt(4 + 2*sqrt(3)): f = radsimp(f)
3+2= 3+1
Here, we have generated an equation, which is a genuine MuPAD object. Another important function is factor, which decomposes an expression into a product of simpler ones:
factor(x^3 + 3*x^2 + 3*x + 1), factor(2*x*y - 2*x - 2*y + x^2 + y^2), factor(x^2/(x + y) - z^2/(x + y))
(x + 1) , (x + y 2) (x + y) ,
(x z) (x + z) (x + y)
The function limit does what its name suggests. For example, the function sin(x)/x has a removable pole at x = 0. Its limit for x 0 is 1:
limit(sin(x)/x, x = 0)
In a MuPAD session, you can define functions of your own in several ways. A simple and intuitive method is to use the arrow operator -> (the minus symbol followed by the greater than symbol):
F := x -> (x^2):
F(x), F(y), F(a + b), F'(x)
x , y , (a + b) , 2 x
In Chapter 17, we discuss MuPAD programming features and describe how to implement more complex algorithms as MuPAD procedures. You can also use the graphics facilities to visualize mathematical objects immediately. The relevant MuPAD functions for generating graphics are the plot command and the routines from the graphics library plot. Here, we present simple calls only, please consult Chapter 11 for in-depth information:
plot(sin(x^2), x = -2.5)
plot(sin(x^2 + y^2), x = 0.PI, y = 0.PI, #3D)
Solving equations or systems of equations is certainly an important task for a computer algebra system. This is done via solve:
equations := {x + y = a, x - a*y = b}: unknowns := {x, y}: solve(equations, unknowns, IgnoreSpecialCases)
]} {[ ab a2 + b ,y = x= a+1 a+1
Here, we have generated a set of two equations and a set of unknowns which we wish to solve for. solve returns the result in terms of simplified equations, from which you can read off the solution. In the above example, there are two more symbolic parameters a and b. This is why we have told solve which of the symbols it should express in terms of the others. The option IgnoreSpecialCases tells MuPAD to ignore the possibility that a could be 1, where the above solution would be incorrect. Without this option, MuPAD returns a complete solution with three branches:
a := email: b := "4you": a.b
email4you Even if the string contains blanks or operator symbols, a valid identifier is generated, which MuPAD displays using the backtick notation mentioned above:
a := email: b := "4you + x": a.b
email4you + x Strings are not identifiers and cannot be assigned a value:
"string" := 1 Error: Invalid left-hand side in assignment [line 1, col 10]
Exercise 4.6: Which of the following names x, x2, 2x, x_t, diff, exp, caution!-!, x-y, Jack&Jill, a_valid_identifier, #1 are valid identifiers?Which of them can be assigned values? Exercise 4.7: Read the help page for solve. Solve the system of equations
x1 + x2 = 1, x2 + x3 = 1,. , x19 + x20 = 1, x20 =
in the unknowns x1 , x2 ,. , x20. Read the help page for assign and assign the values of the solution to the unknowns.
Symbolic Expressions
We say that an object containing symbolic terms such as the equation
0.3 + sin(3) +
f (x, y) =0 5
is an expression. Expressions of domain type DOM_EXPR are probably the most general data type in MuPAD. Expressions are built of atomic components, as all MuPAD objects, and are composed by means of operators. This comprises binary operators, such as the basic arithmetic operations +, -, *, /, ^, and function calls such as sin(), f(), etc.
Operators
MuPAD throughout uses functions to combine or manipulate objects.2 It would be little intuitive, however, to use function calls everywhere,3 say, _plus(a, b) for the addition a + b. For that reason, a variety of important operations is implemented in such a way that you can use the familiar mathematical notation (operator notation) for input. Also the output is given in such a form. In the following, we list the operators for building more complex MuPAD expressions from atoms. The operators +, -, *, / for the basic arithmetic operations and ^ for exponentiation are valid for symbolic expressions as well:
a + b + c, a - b, -a, a*b*c, a/b, a^b
a + b + c, a b, a, a b c,
a b , a b
You may input these operators in the familiar mathematical way, but internally they are function calls:
2 Remarkably, the MuPAD kernel treats not only genuine function calls, such as sin(0.2), assignments, or arithmetical operations in a functional way, but also loops (Chapter 15) and case distinctions (Chapter 16). 3 LISP
programmers may disagree.
_plus(a,b,c), _subtract(a,b), _negate(a), _mult(a,b,c), _divide(a,b), _power(a,b)
The same holds for the factorial of a nonnegative integer. You may input it in the mathematical notation n!. Internally it is converted to a call of the function fact:
i := 0: repeat i := i + 1; if isprime(i) then print(i, "is a prime") else print(i, "is no prime") end_if until i = 3 end_repeat 1, "is no prime" 2, "is a prime" 3, "is a prime"
Here we have used strings enclosed in " for the screen output. They are discussed in detail in Section 4.11. Note that it is not necessary to use the function bool in branching or termination conditions in order to evaluate the condition to TRUE or FALSE. Exercise 4.26: Let denote the logical and, let denote the logical or, let denote logical negation. To which Boolean value does TRUE (FALSE (FALSE FALSE)) evaluate? Exercise 4.27: Let L1, L2 be two MuPAD lists of equal length. How can you find out whether L1[i] < L2[i] holds true for all list elements?
Strings
Strings are pieces of text, which may be used for formatted screen output. A string is a sequence of arbitrary symbols enclosed in string delimiters ". Its domain type is DOM_STRING.
string1 := "Use * for multiplication"; string2 := ", "; string3 := "use ^ for exponentiation."
Use * for multiplication , use for exponentiation. The concatenation operator. combines strings:
string4 := string1.string2.string3
Use * for multiplication, use for exponentiation. The dot operator is a short form of the MuPAD function _concat, which concatenates (arbitrarily many) strings:
_concat("This is ", "a string", ".")
This is a string. The index operator [ ] extracts the characters from a string:
string4[1], string4[2], string4[3], string4[4], string4[5]
U, s, e, , * When using a range as index, substrings are returned; negative indices count from the end of the string:
string4[19.21], string4[-15.-8]
cat, exponent You may use the command print to output intermediate results in loops or procedures on the screen (Section 12.1). By default, this function prints strings 4-49
with the enclosing double quotes. You may change this behavior by using the option Unquoted:
print(string4) "Use * for multiplication, use ^ for exponentiation." print(Unquoted, string4) Use * for multiplication, use ^ for exponentiation.
Strings are not valid identifiers in MuPAD, so you cannot assign values to them:
"name" := sin(x) Error: Invalid left-hand side in assignment [line 1, col 8]
Also, arithmetic with strings is not allowed:
S := expr(t)
x5 + x4 + x3 + x2 + x + 1
Here we have used the system function expr to convert the series to an expression of domain type DOM_EXPR. As you can see in the output, the O() term has been cut off. The op command acts on series in a non-obvious way and should not be used:
0, 1, 0, 6, [1, 1, 1, 1, 1, 1] , x = 0, Undirected
Therefore, the function coeff is provided to extract the coefficients. This is more intuitive than op. The call coeff(t, i) returns the coefficient of xi :
t := taylor(cos(x^2), x, 20)
( ) x4 x8 x12 x16 + + + O x, FAIL 720
coeff(t, 0), coeff(t, 1), coeff(t, 12), coeff(t, 25)
1, 0,
In the previous example, we have supplied x as second argument to specify the point of expansion. This is equivalent to x = 0. The usual arithmetic operations also work for series:
a := taylor(cos(x), x, 3): b := taylor(sin(x), x, 4): a, b
( ) ( ) x2 x3 + O x4 , x + O x6 ( ) ( ) ( ) x2 xx3 + O x4 , 2 x + O x5 , 1 x2 + O x6 3
a + b, 2*a*b, a^2
Both the composition operator @ and the iteration operator @@ apply to series as well:
a := taylor(sin(x), x, 20): b := taylor(arcsin(x), x, 20): a@b
( ) x + O x21
If you try to compute the Taylor series of a function that does not have one, then taylor aborts with an error. The function series can compute more general expansions (Laurent series, Puiseux series):
taylor(cos(x)/x, x = 0, 10) Error: 1/x*cos(x) does not have a Taylor series \ expansion, try 'series' [taylor] series(cos(x)/x, x = 0, 10)
( ) x5 xx x3 + + + O x9 x 720 40320
You can generate series expansions in terms of negative powers by expanding around the point infinity:
series((x^2 + 1)/(x + 1), x = infinity)
( ) 1 x1+ 2 + +O 5 x x x x x
This is an example for an asymptotic expansion, which approximates the behavior of a function for large values of the argument. In simple cases, series returns an expansion in terms of negative powers of x, but other functions may turn up as well:
series((exp(x) - exp(-x))/(exp(x) + exp(-x)), x = infinity)
( ) + O e12 x
Exercise 4.33: The order p of a root x of a function f is the maximal number of derivatives that vanish at the point x:
f (x) = f (x) = = f (p1) (x) = 0 ,
f (p) (x) = 0.
What is the order of the root x = 0 of f (x) = tan(sin(x)) sin(tan(x))? Exercise 4.34: Besides the arithmetical operators, some other system functions such as diff or int work directly for series. Compare the result of taylor(diff(1/(1 - x), x), x) and the derivative of taylor(1/(1 - x), x). Mathematically, both series are identical. Can you explain the difference in MuPAD?
Exercise 4.35: The function f (x) = x + 1 x 1 tends to zero for large x, i.e., limx f (x) = 0. Show that the approximation f (x) 1/ x is valid for large values of x. Find better asymptotic approximations of f.
cos((x)) e(x) , sin((x)) e(x) As a basic principle, rectform regards all symbolic identifiers as representatives of complex numbers. However, you can use assume (Section 9.3) to specify that an identifier represents only real numbers:
assume(a in R_): z := rectform(a*b + I)
a (b) + a (b) i + i
The results of rectform have a special data type:
domtype(z)
rectform You can use the function expr to convert such an object to a normal expression of domain type DOM_EXPR:
expr(z)
a (b) i + a (b) + i
Simplifying Expressions
In some cases, a transformation leads to a simpler expression:
f := 2^x*3^x/8^x/9^x: f = combine(f)
2x 3x = 8x 9x
f := x/(x + y) + y/(x + y): f = normal(f)
x y + =1 x+y x+y
To this end, however, you must inspect the expression and decide yourself which function to use for simplification. There are tools for applying various simplification algorithms to an expression automatically: the functions simplify and Simplify. These are universal simplifiers which MuPAD uses to achieve a representation of an expression that is as simple as possible:
f := 2^x*3^x/8^x/9^x: f = simplify(f)
1 2x 3x = 2x x x 9x 3
f := (1 + (sin(x)^2 + cos(x)^2)^2)/sin(x): f = simplify(f)
cos(x) + sin(x) sin(x)
)2 +1 = 2 sin(x)
f := x/(x + y) + y/(x + y) - sin(x)^2 - cos(x)^2: f = simplify(f)
x y sin(x) cos(x) + =0 x+y x+y
f := (exp(x) - 1)/(exp(x/2) + 1): f = simplify(f)
x ex 1 = ex e2 + 1
f := sqrt(997) - (997^3)^(1/6): f = simplify(f)
Note that simplify operates in a purely heuristic way since there is no general answer what simple means. You can control the simplification process by supplying additional arguments. As in the case of combine, you can request particular simplifications by means of options. For example, you can tell the simplifier explicitly to simplify expressions containing square roots:
f := sqrt(4 + 2*sqrt(3)): f = simplify(f, sqrt)
The possible options are exp, ln, log, cos, sin, cosh, sinh, sqrt, gamma, unit, condition, logic, and relation. Internally, simplify then confines itself to those simplification rules that are valid for the function given as option. The options logic and relation are for simplifying logical expressions and equations and inequalities, respectively (see also the corresponding help page: ?simplify). Instead of simplify(expression, sqrt), you may also use the function radsimp to simplify numerical expressions containing square roots or other radicals:
Alternatively, the environment variable WRITEPATH can be set:
WRITEPATH := "C:\\Documents": plot(Primitive1, Primitive2,., OutputFile = "mypicture.xvz")
Now, the plot data are stored in the file C:\Documents\mypicture.xvz. Apart from saving files as xml data, MuPAD pictures can also be saved in a variety of standard graphical formats such as jpg, eps, svg, bmp etc. In batch mode, the export is triggered by the OutputFile attribute in the same way as for saving in xml format. Just use an appropriate extension of the filename indicating the format. The following commands save the plot in four different files in jpg, eps, svg, and bmp format, respectively:
plot(Primitive1, plot(Primitive1, plot(Primitive1, plot(Primitive1,.,.,.,., OutputFile OutputFile OutputFile OutputFile = = = = "mypicture.jpg"): "mypicture.eps"): "mypicture.svg"): "mypicture.bmp"):
11-108
An animated MuPAD plot can be exported to avi format:
plot(plot::Function2d(exp(a*x), x = 0.1, a = -1.1) OutputFile = "myanimation.avi")
If no file extension is specified by the file name, the default extension xvc is used, i.e., uncompressed xml data are written. If a notebook is saved to a file, its location in the file system is available inside the notebook as the value of the environment variable NOTEBOOKPATH. If you wish to save your plot in the same folder as the notebook, you may call
plot(Primitive1, Primitive2,., OutputFile = NOTEBOOKPATH."mypicture.xvz")
In addition to OutputFile, there is the attribute OutputOptions to specify parameters for some of the export formats. The help page of this attribute provides detailed information.
11-109
Importing Pictures
MuPAD does not provide for many tools to import standard graphical vector formats, yet. Presently, the only supported vector type is the stl format, popular in stereo lithography, which encodes 3D surfaces. It can be imported via the routine plot::SurfaceSTL. In contrast to graphical vector formats, there are many standard bitmap formats such as bmp, gif, jpg, ppm etc. that can be imported. One can read such a file via import::readbitmap, thus creating a MuPAD array of RGB color values or an equivalent three-dimensional hardware float array that can be manipulated at will. In particular, it can be fed into the routine plot::Raster which creates an object that can be used in any 2D MuPAD plot. Note, however, that the import of bitmap data consumes a lot of memory, i.e., only reasonably small bitmaps (up to a few hundred pixels in each direction) should be processed. Memory consumption is much smaller with hardware floating point arrays, which is why import::reasonably uses them by default. In the following example, we plot the probability density function and the cumulative density function of the standard normal (Gaussian) distribution. Paying tribute to Carl Friedrich Gauss, we wish to display his picture in this plot. Assume that we have his picture as a png bitmap file Gauss.png. We import the file via import::readbitmap that provides us with the width and height in pixels and the color data:
[width, height, gauss] := import::readbitmap("Gauss.png"):
Note the colon at the end of the above command! Without it, MuPAD will print the color information on the screen, and formatting such a huge output takes time. We have to use Scaling = Constrained to preserve the aspect ratio of the image. Unfortunately, this setting is not appropriate for the function plots. So we use two different scenes that are positioned via Layout = Relative. See Section Layout of Canvas and Scenes of the online plot documentation for details on how the layout of a canvas containing several scenes is set.
pdf := stats::normalPDF(0, 1): cdf := stats::normalCDF(0, 1):
11-110
plot(plot::Scene2d(plot::Function2d(pdf(x), x = -4.7), plot::Function2d(cdf(x), x = -4.7), Width = 1, Height = 1), plot::Scene2d(plot::Raster(gauss), Scaling = Constrained, Width = 0.3, Height = 0.6, Bottom = 0.25, Left = 0.6, BorderWidth = 0.5*unit::mm, Footer = "C.F. Gauss", FooterFont = [8]), Layout = Relative)
11-111
Cameras in 3D
The MuPAD 3D graphics model includes an observer at a specific position, pointing a camera with a lens of a specific opening angle to some specific focal point. The specific parameters position, angle, and focal point determine the picture that the camera will take. When a 3D picture is created,a camera with an appropriate default lens is positioned automatically. Its focal point is chosen as the center of the graphical scene. The interactive viewer allows to rotate the scene which, in fact, is implemented internally as a change of the camera position. Also interactive zooming in and zooming out is realized by moving the camera closer to or farther away from the scene. Apart from interactive camera motions, the perspective of a 3D picture can also be set in the calls generating the plot. One way is to specify the direction from which the camera is pointing towards the scene. This is done via the attribute CameraDirection:
plot(plot::Function3d(sin(x + y^3), x = -1.1, y = -1.1), CameraDirection = [-25, 20, 30])
11-112
plot(plot::Function3d(sin(x + y^3), x = -1.1, y = -1.1), CameraDirection = [10, -40, 10])
In these calls, CameraDirection does not fully specify the position of the camera. This attribute just requests the camera to be placed at some large distance from the scene along the ray in the direction given by the attribute. The actual distance from the scene is determined automatically to let the graphical scene fill the picture optimally. For a full specification of the perspective, there are camera objects of type plot::Camera that allow to specify the position of the camera, its focal point and the opening angle of its lens:
position := [-5, -10, 5]: focalpoint := [0, 0, 0]: angle := PI/12: camera := plot::Camera(position, focalpoint, angle):
This camera can be passed like any graphical object to the plot command generating the scene.
11-113
Once a camera object is specified in a graphical scene, it determines the view. No automatic camera is used any more:
plot(plot::Function3d(sin(x + y^3), x = -1.1, y = -1.1), camera)
11-114
Camera objects can be animated:
camera := plot::Camera([3*cos(a), 3*sin(a), 1 + cos(2*a)], [0, 0, 0], PI/3, a = 0.2*PI, Frames = 100):
Inserting the animated camera in a graphical scene, we obtain an animated plot simulating a flight around the scene:
In fact, several cameras can be installed simultaneously in a scene. Per default, the first camera produces the view rendered. After clicking on another camera in the object browser of the viewer (page 11-50), the selected camera takes over and the new view is shown.
11-115
Next, we have a look at a more appealing example: the so-called Lorenz attractor. The Lorenz ODE is the system x p (y x) d = y x z + r x y dt z xybz with fixed parameters p, r, b. As a dynamical system for Y = [x, y, z], we have to solve the ODE dY /dt = f (t, Y ) with the following vector field:
f := proc(t, Y) local x, y, z; begin [x, y, z] := Y: [p*(y - x), -x*z + r*x - y, x*y - b*z] end_proc:
Consider the following parameters and the following initial condition Y0:
p := 10: r := 28: b := 1: Y0 := [1, 1, 1]:
The routine plot::Ode3d serves for generating a graphical 3D solution of a dynamical system. It solves the ODE numerically and generates graphical data from the numerical mesh. The plot data are specified by the user via generators (procedures) that map a solution point (t, Y ) to a point (x, y, z) in 3D. The following generator Gxyz produces a 3D phase plot of the solution. The generator Gyz projects the solution curve to the (y, z) plane with x = 20; the generator Gxz projects the solution curve to the (x, z) plane with y = 20; the generator Gxy projects the solution curve to the (x, y) plane with z = 0:
Printing Expressions on the Screen
The function print outputs MuPAD objects on the screen:
for i from 4 to 5 do print("The ", i, "th prime is ", ithprime(i)) end_for "The ", 4, "th prime is ", 7 "The ", 5, "th prime is ", 11
We recall that ithprime(i) computes the i-th prime. MuPAD encloses the text in double quotes. Use the option Unquoted to suppress this:
for i from 4 to 5 do print(Unquoted, "The ", i, "th prime is ", ithprime(i)) end_for The , 4, th prime is , 7 The , 5, th prime is , 11
Note that the option Unquoted breaks typesetting:
print(x^2); print(Unquoted, x^2)
Furthermore, you can eliminate the commas from the output by means of the tools for manipulating strings that are presented in Section 4.11:
for i from 4 to 5 do print(Unquoted, "The ". expr2text(i). "th prime is ". expr2text(ithprime(i)). ".") end_for The 4th prime is 7. The 5th prime is 11.
Here, the function expr2text is used to convert the value of i and the prime returned by ithprime(i) to strings. Then, the concatenation operator. combines them with the other strings to a single string. Note that converting an expression to a string breaks typesetting as well as the pretty print format explained below:
print(Unquoted, expr2text(x^2)) x^2
Alternatively you can use the function fprint, which writes data to a file or on the screen. In contrast to print, it does not output its arguments as individual expressions. Instead, fprint combines them to a single string (if you use the option Unquoted):
a := one: b := string:
fprint(Unquoted, 0, "This is ", a, " ", b) This is one string
The second argument 0 tells fprint to direct its output to the screen.
Modifying the Output Format
Usually, MuPAD will format outputs in a way similar to how mathematical formulas are usually written inbooks or on a blackboard (typeset expressions): integral signs are displayed as , symbolic sums appear as etc. This is the format used in most result regions of this book. There are two other output formats available. The following text refers to these ASCII-based output only, which are used if the typesetting is switched off via the corresponding menu of the notebook interface or when print with the option Plain is used. Without typesetting, MuPAD usually prints expressions in a two-dimensional form with simple (ascii) characters:
A := x/(x - 1) > 0: x := 1: (if x <> 1 and A then right else wrong end_if), (if x = 1 or A then right else wrong end_if)
MuPAD provides the essential constructs of a programming language. The user can implement complex algorithms comfortably in MuPAD. Indeed, most of MuPADs mathematical intelligence is not implemented in C or C++ within the kernel, but in the MuPAD programming language at the library level. The programming features are more extensive than in other languages such as C, Pascal, or Fortran, since the MuPAD language offers more general and more flexible constructs. We have already presented basic structures such as loops (Chapter 15), branching instructions (Chapter 16), and simple functions (page 4-52). In this chapter, we regard programming as writing complex MuPAD procedures. In principle the user recognizes no differences between simple functions generated via -> (page 4-52) and more complex procedures as presented in this chapter. Procedures, like functions, return values. Only the way of generating such procedure objects via proc end_proc is a little more complicated. Procedures provide additional functionality: there is a distinction between local and global variables, you can use arbitrarily many commands in a clear and convenient way etc. As soon as a procedure is implemented and assigned to an identifier, you may call it in the form procedureName(arguments) like any other MuPAD function. After executing the implemented algorithm, it returns an output value. You can define and use MuPAD procedures within an interactive session, like any other MuPAD object. Typically, however, you want to use these procedures again in later sessions, in particular when they implement more complex algorithms. Then it is useful to write the procedure definition into a text file using your favorite text editor (such as, e.g., the MuPAD source code editor), and read it into
a MuPAD session via read (page 12-5) or with the entry Read commands from the Notebook menu. Apart from the evaluation level, the MuPAD kernel processes the commands in the file exactly in the same way as if they were entered interactively. MuPAD includes an editor with syntax highlighting for MuPAD programs.
Dening Procedures
The following function, which compares two numbers and returns their maximum, is an example of a procedure definition via proc end_proc:
maximum := proc(a, b) /* comment: maximum of a and b */ begin if a<b then return(b) else return(a) end_if; end_proc:
The text enclosed between /* and */ is a comment1 and completely ignored by the system. This is a useful tool for documenting the source code when you write the procedure definition in a text file. The above sample procedure contains an if statement as the only command. More realistic procedures contain many commands (separated by colons or semicolons). The command return terminates a procedure and passes its argument as output value to the system. A MuPAD object generated via proc end_proc is of domain type DOM_PROC:
In such a situation, it is often difficult to locate the source of the error. However, an even worse scenario might happen: if the procedure does not abort, the result is likely to be wrong! Thus, type checking helps to avoid errors. In the above example, we might add a type check of the form
if domtype(A) <> DOM_ARRAY or domtype(B) <> DOM_ARRAY then error("arguments must be of type DOM_ARRAY") end_if
to the procedure body. Starting on page 17-20, we discuss a simpler type checking concept.
Subprocedures
Often tasks occur frequently within a procedure and you want to implement them again in the form of a procedure. This structures and simplifies the program code. In many cases, such a procedure is used only from within a single procedure. Then it is reasonable to define this procedure locally as a subprocedure only in the scope of the calling procedure. In MuPAD you can use local variables to implement subprocedures. If you want to make
g := proc() begin. end_proc:
a local procedure of
f := proc() begin. end_proc:
define f as follows:
f := proc() local g; begin g := proc() begin. end_proc;
/* subprocedure */
/* main part of f, which calls g(.): */. end_proc:
Now, g is a local procedure of f and you can use it only from within f. We give an example. You can implement matrix multiplication by means of suitable columnrow multiplications: ( ) ( ) (2, 1) ( ) ( ) (2, 1) ( ) =. ( ) ( ) = (5, 3) (5, 3) More generally, if we partition the input matrices by rows ai and columns bj , respectively, then a1 a1 b1. a1 bn . . . (b1 ,. , bn ) = ,. am am b1. am bn 17-14
with the inner product
ai bj =
(ai )r (bj )r.
We now write a procedure MatrixMult that expects input arrays A and B of the form array(1.m, 1.k) and array(1.k, 1.n), and returns the m n matrix product A B. A call of the subprocedure RowTimesColumn with arguments i, j extracts the i-th row and the j -th column from the input matrices A and B , respectively, and computes the inner product of the row and the column. The subprocedure uses the arrays A, B as well as the locally declared dimension parameters m, n, and k as global variables:
d ab d b ln(a) d = e = eb ln(a) (b ln(a)) dx dx dx
= ab ln(a)
db da + ab1 b , dx dx (chain rule).
d dy F (y(x)) = F (y) dx dx
Moreover, for some functions F , the derivative is known, and we want to take this into account in our implementation. For an unknown function F , we return the symbolic function call of the differentiation routine. 17-38
The procedure Diff in Table 17.1 implements the above properties in the stated order. Its calling syntax is Diff(expression, identifier).
Diff := proc(f, x : DOM_IDENT) // local a, b, F, y; begin if not has(f, x) then return(0) end_if; // if f = x then return(1) end_if; // if type(f) = "_plus" then return(map(f, Diff, x)) end_if; // if type(f) = "_mult" then a := op(f, 1); b := subsop(f, 1 = 1); return(Diff(a, x)*b + a*Diff(b, x)) // end_if; if type(f) = "_power" then a := op(f, 1); b := op(f, 2); return(f*ln(a)*Diff(b, x) + a^(b - 1)*b*Diff(a, x)) // end_if; if op(f, 0) <> FAIL then F := op(f, 0); y := op(f, 1); // if F = hold(exp) then return( exp(y)*Diff(y, x)) end_if; // if F = hold(ln) then return( 1/y *Diff(y, x)) end_if; // if F = hold(sin) then return( cos(y)*Diff(y, x)) end_if; // if F = hold(cos) then return(-sin(y)*Diff(y, x)) end_if; // /* specify further known functions here */ end_if; procname(args()) // end_proc: (0) (1) (2) (3)
(6) (6) (6) (6) (6)
Table 17.1: A symbolic differentiation routine In (0), we implemented an automatic type check of the second argument, which must be a symbolic identifier of domain type DOM_IDENT. In (1), the MuPAD function has checks whether the expression f to be differentiated depends on x. Linearity of differentiation is implemented in (3) by means of the MuPAD 17-39
function map:
map(f1(x) + f2(x) + f3(x), Diff, x)
Diff(f1(x) , x) + Diff(f2(x) , x) + Diff(f3(x) , x) In (4), we handle a product expression f = f1 f2 . : the command a:= op(f,1) determines the first factor a = f1 , then subsop(f, 1 = 1) replaces this factor by 1, such that b assumes the value f2 f3 . Then, we call Diff(a, x) and Diff(b, x). If b = f2 f3 . is itself a product, this leads to another execution of step (4) at the next recursion level. In this way, (4) handles products of arbitrary length. Step (5) differentiates powers. For f = ab , the call op(f, 1) returns the base a and op(f, 2) the exponent b. In particular, this covers all monomial expressions of the form f = xn for constant n. The recursive calls to Diff for a = x and b = n then yield Diff(a, x) = 1 and Diff(b, x) = 0, respectively, and the expression returned in (5) simplifies to the correct result n xn1. If the expression f is a symbolic function call of the form f = F (y), we extract the outer function F in (6) via F:= op(f, 0) (otherwise, F gets the value FAIL). Next, we handle the case where F is a function with one argument y and extract the inner function by y:= op(f, 1). If F is the name of a function with known derivative (such as F = exp, ln, sin, cos), then we apply the chain rule. It is easy to extend this list of functions F with known derivatives. In particular, you can add a formula for differentiating symbolic expressions of the form int(, ). Extensions to functions F with more than one argument are also possible. Finally, step (7) returns Diff(f, x) symbolically if no simplifications of the expression f happen in steps (1) through (6).
Exercise 4.41:
p := poly(x^7 - x^4 + x^3 - 1): q := poly(x^3 - 1): p - q^2
( ) poly x7 x6 x4 + 3 x3 2, [x]
The polynomial p is a multiple of q :
( ) poly x4 + 1, [x]
This is confirmed by a factorization:
factor(p)
( ) ( ) poly (x 1, [x]) poly x2 + x + 1, [x] poly x4 + 1, [x] ( ) poly (x 1, [x]) poly x2 + x + 1, [x]
factor(q)
Exercise 4.42: We use the identifier R to abbreviate the lengthy type name Dom::IntegerMod(3). With alias, MuPAD also uses R as an alias in the output of the following polynomials.
p := 3: alias(R = Dom::IntegerMod(p)):
We only need to try the possible remainders 0, 1, 2 modulo 3 for the coefficients a, b, c in a x2 + b x + c. We generate a list of all 18 quadratic polynomials with a = 0 as follows:
[((poly(a*x^2 + b*x + c, [x], R) $ a = 1.p-1) $ b = 0.p-1) $ c = 0.p-1]:
The command select(, irreducible) extracts the 6 irreducible polynomials:
select(%, irreducible) [poly(x 2 + 1, [x], R), poly(2 x + x + 1, [x], R),
poly(2 x
+ 2 x + 1, [x], R), 2
poly(2 x 2
+ 2, [x], R), poly(x
+ x + 2, [x], R),
poly(x
+ 2 x + 2, [x], R)]
Exercise 5.1: The value of x is the identifier a1. The evaluation of x yields the identifier c1. The value of y is the identifier b2. The evaluation of y yields the identifier c2. The value of z is the identifier a3. The evaluation of z yields 10. The evaluation of u1 leads to an infinite recursion, which MuPAD aborts with an error message. The evaluation of u2 yields the expression v2^2 - 1. Exercise 6.1: The result of subsop(b + a, 1 = c) is b + c and not c + a, as you might have expected. The reason is that subsop evaluates its arguments. The system reorders the sum internally when evaluating it, and thus subsop processes a + b instead of b + a. Upon return, the result c + b is reordered again.
Exercise 6.2: The highest derivative occurring in g is the 6-th derivative diff(f(x), x $ 6). We pass the sequence of replacement equations:
diff(f(x), x $ 6) = f6, diff(f(x), x $ 5) = f5,., diff(f(x), x) = f1, f(x) = f0
to MuPADs substitution function. Note that, in accordance with the usual mathematical notation, diff returns the function itself as the 0-th derivative: diff(f(x), x $ 0) = diff(f(x)) = f(x).
delete f: g := diff(f(x)/diff(f(x), x), x $ 5): subs(g, (diff(f(x), x $ 6 - i) = f.(6-i)) $ i = 0.6) 60 ffff0 ff2 f3 ------ - ---- + ------ - ---------- - ---------- 4 ff1 f1 f1 f1 f0 ff2 ff0 f2 ff0 f3 f4 ----- + -------- + ----------- + ----------- f1 f1 f1 f90 f0 f2 ff0 f2 ff0 f2 f4 ------------ + ------------- - -----------4 f1 f1 f1

Creating Symbolic Objects with Identical Names
If you set a variable equal to a symbolic expression, and then apply the syms command to the variable, MATLAB software removes the previously defined expression from the variable. For example,
syms a b; f = a + b
f = a + b
If later you enter
syms f; f
then MATLAB removes the value a + b from the expression f:
You can use the syms command to clear variables of definitions that you previously assigned to them in your MATLAB session. However, syms does not clear the following assumptions of the variables: complex, real, and positive. These assumptions are stored separately from the symbolic object. See Deleting Symbolic Objects and Their Assumptions on page 2-32 for more information.
Creating a Matrix of Symbolic Variables
Using Existing Symbolic Objects as Elements
A circulant matrix has the property that each row is obtained from the previous one by cyclically permuting the entries one step forward. For example, create the symbolic circulant matrix whose elements are a, b, and c, using the commands:
syms a b c; A = [a b c; c a b; b c a] A [ [ [ = a, b, c] c, a, b] b, c, a]
Since matrix A is circulant, the sum of elements over each row and each column is the same. Find the sum of all the elements of the first row:
sum(A(1,:)) ans = a + b + c
Check if the sum of the elements of the first row equals the sum of the elements of the second column:
sum(A(1,:)) == sum(A(:,2))
The sums are equal:
ans = 1
From this example, you can see that using symbolic objects is very similar to using regular MATLAB numeric objects.
Generating Elements While Creating a Matrix
The sym function also lets you define a symbolic matrix or vector without having to define its elements in advance. In this case, the sym function generates the elements of a symbolic matrix at the same time when it creates a matrix. The function presents all generated elements using the same form: the base (which must be a valid variable name), a row index, and a column index. Use the first argument of sym to specify the base for the names of generated elements. You can use any valid variable name as a base. To check whether the name is a valid variable name, use the isvarname function. By default, sym separates a row index and a column index by underscore. For example, create the 2-by-4 matrix A with the elements A1_1,., A2_4:
collect
The statementcollect(f) views f as a polynomial in its symbolic variable, say x, and collects all the coefficients with the same power of x. A second argument can specify the variable in which to collect terms if there is more than one candidate. Here are a few examples. f
syms x; f = (x-1)*(x-2)*(x-3);
collect(f)
collect(f) ans = x^3 - 6*x^2 + 11*x - 6 collect(f) ans = x^3 - 6*x^2 + 11*x - 6 collect(f) ans = (2*t)*x + t
syms x; f = x*(x*(x - 6) + 11) - 6;
syms x t; f = (1+x)*t + x*t;
expand
The statement expand(f) distributes products over sums and applies other identities involving functions of sums as shown in the examples below. f
syms a x y; f = a*(x + y);
expand(f)
expand(f) ans = a*x + a*y syms x; f = (x - 1)*(x - 2)*(x - 3); expand(f) ans = x^3 - 6*x^2 + 11*x - 6 expand(f) ans = x^3 - 6*x^2 + 11*x - 6 expand(f) ans = exp(a)*exp(b) syms x y; f = cos(x + y); expand(f) ans = cos(x)*cos(y) - sin(x)*sin(y)
syms x; f = x*(x*(x 6) + 11) - 6;
syms a b; f = exp(a + b);
syms x; f = cos(3*acos(x));
expand(f) ans = 4*x^3 - 3*x expand(f) ans = 4*x^3 - 3*x
syms x; f = 3*x*(x^2 1) + x^3;
horner
The statement horner(f) transforms a symbolic polynomial f into its Horner, or nested, representation as shown in the following examples. f
syms x; f = x^3 - 6*x^2 + 11*x - 6;
horner(f)
horner(f) ans = x*(x*(x - 6) + 11) - 6 horner(f) ans = x*((33*x)/10 + 11/5) + 11/10
syms x; f = 1.1 + 2.2*x + 3.3*x^2;
factor
If f is a polynomial with rational coefficients, the statement
factor(f)
expresses f as a product of polynomials of lower degree with rational coefficients. If f cannot be factored over the rational numbers, the result is f itself. Here are several examples.
factor(f) ans = (x - 3)*(x - 1)*(x - 2) factor(f) ans = x^3 - 6*x^2 + 11*x - 5 factor(f) ans = (x^2 + 1)*(x^4 - x^2 + 1)
syms x; f = x^3 - 6*x^2 + 11*x - 5;
syms x; f = x^6 + 1;
Here is another example involving factor. It factors polynomials of the form x^n + 1. This code
syms x; n = (1:9)'; p = x.^n + 1; f = factor(p); [p, f]
returns a matrix with the polynomials in its first column and their factored forms in its second.
Since you did not specify the dependent variables, solve uses symvar to determine the variables. This way of assigning output from solve is quite successful for small systems. Plainly, if you had, say, a 10-by-10 system of equations, typing
[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10] = solve(.)
is both awkward and time consuming. To circumvent this difficulty, solve can return a structure whose fields are the solutions. In particular, consider the system u^2 - v^2 = a^2, u + v = 1, a^2 - 2*a = 3. The command
S = solve('u^2 - v^2 = a^2', 'u + v = 1', 'a^2 - 2*a = 3')
S = a: [2x1 sym] u: [2x1 sym] v: [2x1 sym]
The solutions for a reside in the a-field of S. That is,
ans = -1 3
Similar comments apply to the solutions for u and v. The structure S can now be manipulated by field and index to access a particular portion of the solution. For example, if you want to examine the second solution, you can use the following statement
s2 = [S.a(2), S.u(2), S.v(2)]
to extract the second component of each field.
s2 = [ 3, 5, -4]
The following statement
M = [S.a, S.u, S.v]
creates the solution matrix M
M = [ -1, 1, 0] [ 3, 5, -4]
whose rows comprise the distinct solutions of the system. Linear systems of simultaneous equations can also be solved using matrix division. For example,
clear u v x y
syms u v x y S = solve(x + 2*y - u, 4*x + 5*y - v); sol = [S.x; S.y] A = [1 2; 4 5]; b = [u; v]; z = A\b
results in
sol = (2*v)/3 - (5*u)/3 (4*u)/3 - v/3 z = (2*v)/3 - (5*u)/3 (4*u)/3 - v/3
Thus s and z produce the same solution, although the results are assigned to different variables.
Single Differential Equation
The function dsolve computes symbolic solutions to ordinary differential equations. The equations are specified by symbolic expressions containing the letter D to denote differentiation. The symbols D2, D3,. DN, correspond to the second, third,., Nth derivative, respectively. Thus, D2y is the toolbox equivalent of d2y/dt2. The dependent variables are those preceded by D and the default independent variable is t. Note that names of symbolic variables should not contain D. The independent variable can be changed from t to some other symbolic variable by including that variable as the last input argument. Initial conditions can be specified by additional equations. If initial conditions are not specified, the solutions contain constants of integration, C1, C2, etc. The output from dsolve parallels the output from solve. That is, you can call dsolve with the number of output variables equal to the number of dependent variables or place the output in a structure whose fields contain the solutions of the differential equations.
Example 1
The following call to dsolve
dsolve('Dy = t*y')
uses y as the dependent variable and t as the default independent variable. The output of this command is
ans = C2*exp(t^2/2) y = C*exp(t^2/2) is a solution to the equation for any constant C.
To specify an initial condition, use
y = dsolve('Dy = t*y', 'y(0) = 2')
This produces
y = 2*exp(t^2/2)
Notice that y is in the MATLAB workspace, but the independent variable t is not. Thus, the command diff(y,t) returns an error. To place t in the workspace, enter syms t.
Example 2
Nonlinear equations may have multiple solutions, even when initial conditions are given:
x = dsolve('(Dx + x)^2 = 1', 'x(0) = 0')
x = 1/exp(t) - - 1/exp(t)
Example 3
Here is a second-order differential equation with two initial conditions, and the default independent variable changed to x. The commands
y = dsolve('D2y = cos(2*x) - y', 'y(0) = 1', 'Dy(0) = 0', 'x'); simplify(y)
ans = 1 - (8*(cos(x)/2 - 1/2)^2)/3
Example 4
The key issues in this example are the order of the equation and the initial conditions. To solve the ordinary differential equation
d3u dx3
u(0) = 1, u(0) = 1, u(0) = ,
with x as the independent variable, type
u = dsolve('D3u = u',. 'u(0) = 1', 'Du(0) = -1', 'D2u(0) = pi', 'x')
Use D3u to represent d3u/dx3 and D2u(0) for u(0).
u = (pi*exp(x))/3 - (cos((3^(1/2)*x)/2)*(pi/3 - 1))/exp(x/2). - (3^(1/2)*sin((3^(1/2)*x)/2)*(pi + 1))/(3*exp(x/2))
Further ODE Examples
This table shows a few more examples of differential equations and their Symbolic Math Toolbox syntax. The final entry in the table is the Airy differential equation, whose solution is referred to as the Airy function.
Differential Equation
y = dsolve('Dy+4*y = exp(-t)', 'y(0) = 1')
dy + 4 y(t) = et dt
y(0) = 1 2x2y + 3xy y = 0 ( = d/dx)
y = dsolve('2*x^2*D2y + 3*x*Dy - y = 0', 'x') y = dsolve('D2y = x*y', 'y(0) = 0', 'y(3) = besselk(1/3, 2*sqrt(3))/pi', 'x')
d2 y dx2
= xy( x) 1 K1 / 3 (2 3)
y(0) = 0, y(3) =
1 Enable plot editing mode in the MATLAB figure window. 2 Double-click any element on the graph.
For information about editing object properties in plot editing mode, see The Property Editor. If you prefer to work from the MATLAB command line or if you want to create a code file, you can edit graphs by using MATLAB commands. For information about command-line graph editing, see Understanding Handle Graphics Objects. Also, you can combine the interactive and command-line editing approaches to achieve the look you want for the graphs you create.
Saving Graphs
After you create, edit, and explore a function plot, you might want to save the result. MATLAB provides three different ways to save graphs: Save a graph as a MATLAB FIG-file (a binary format). The FIG-file stores all information about a graph, including function plots, graph data, annotations, data tips, menus and other controls. You can open the FIG-file only with MATLAB. For more information, see Saving a Graph in FIG-File Format. Export a graph to a different file format. When saving a graph, you can choose a file format other than FIG. For example, you can export your graphs to EPS, JPEG, PNG, BMP, TIFF, PDF, and other file formats. You can open the exported file in an appropriate application. For more information, see Saving to a Different Format Exporting Figures.
Print a graph on paper or print it to file. To ensure the correct plot size, position, alignment, paper size and orientation, use Print Preview. For more information, see Printing Figures. Generate a MATLAB file from a graph. You can use the generated code to reproduce the same graph or create a similar graph using different data. This approach is useful for generating MATLAB code for work that you have performed interactively with the plotting tools. For more information, see Generating a MATLAB File to Recreate a Graph.
Generating Code from Symbolic Expressions
In this section. Generating C or Fortran Code on page 3-134 Generating MATLAB Functions on page 3-135 Generating MATLAB Function Blocks on page 3-140 Generating Simscape Equations on page 3-144
Generating C or Fortran Code
You can generate C or Fortran code fragments from a symbolic expression, or generate files containing code fragments, using the ccode and fortran functions. These code fragments calculate numerical values as if substituting numbers for variables in the symbolic expression. To generate code from a symbolic expression g, enter either ccode(g) or fortran(g). For example:
The MuPAD definition of Fourier transform and inverse Fourier transform differ from their Symbolic Math Toolbox counterparts by the sign of the exponent: Symbolic Math Toolbox Definition Fourier transform
MuPAD Definition
F f (w)
f ( x) e
f ( x) eiwx dx.
Corresponding code is:
F = fourier(f)
F := transform::fourier(f,x,w)
Inverse Fourier transform
F 1 f ( x)
Finv = ifourier(f)
Finv := transform::invfourier(f,w,x)
The MuPAD definition of exponential integral differs from the Symbolic Math Toolbox counterpart. Symbolic Math Toolbox Definition Exponential integral expint(x) = Ei(x) =
exp(t) t dt for x > 0 =
Ei( x) =
et dt for x < 0. t
Ei(1, x).
Ei(n, x) =
exp( xt) tn
The definitions of Ei extend to the complex plane, with a branch cut along the negative real axis.
Copying Variables and Expressions Between the MATLAB Workspace and MuPAD Notebooks
You can copy a variable in a MuPAD notebook to a variable in the MATLAB workspace using a MATLAB command. Similarly, you can copy a variable or symbolic expression in the MATLAB workspace to a variable in a MuPAD notebook using a MATLAB command. To do either assignment, you need to know the handle to the MuPAD notebook you want to address. The only way to assign variables between a MuPAD notebook and the MATLAB workspace is to open the notebook using the following syntax:
nb = mupad;
You can use any variable name for the handle nb. TO open an existing notebook file, use the following syntax:
nb = mupad(file_name);
Here file_name must be a full path unless the notebook is in the current folder. The handle nb is used only for communication between the MATLAB workspace and the MuPAD notebook. To copy a symbolic variable in the MATLAB workspace to a variable in the MuPAD notebook engine with the same name, enter this command in the MATLAB Command Window:
setVar(notebook_handle,variable)
For example, if nb is the handle to the notebook and z is the variable, enter:
setVar(nb,z)
There is no indication in the MuPAD notebook that variable z exists. Check that it exists by entering z in an input area of the notebook, or by entering the command anames(All, User) in the notebook. To assign a symbolic expression to a variable in a MuPAD notebook, enter:
setVar(notebook_handle,'variable',expression)
at the MATLAB command line. For example, if nb is the handle to the notebook, exp(x) - sin(x) is the expression, and z is the variable, enter:
syms x setVar(nb,'z',exp(x) - sin(x))
For this type of assignment, x must be a symbolic variable in the MATLAB workspace. Again, there is no indication in the MuPAD notebook that variable z exists. Check that it exists by entering z in an input area of the notebook, or by entering the command anames(All, User) in the notebook. To copy a symbolic variable in a MuPAD notebook to a variable in the MATLAB workspace, enter in the MATLAB Command Window:
For details, see Checking a Variables Assumptions on page 4-53 and Examples of the Effect of Assumptions on page 4-54. If you reset the symbolic engine by entering the command
reset(symengine)
or if you change symbolic engines with the symengine command, MATLAB no longer recognizes any symbolic variables that exist in the MATLAB workspace. Clear the variables with the clear command, or renew them with the syms or sym command. This example shows how the MATLAB workspace and the symbolic engine workspace respond to a sequence of commands. Step Command
syms x positive clear x syms x syms x clear
MATLAB Workspace
MuPAD Engine Workspace
x is positive x is positive x is positive
Checking a Variables Assumptions
To check whether a variable, say x, has any assumptions in the symbolic engine workspace associated with the MATLAB workspace, enter the following command in the MATLAB Command Window:
evalin(symengine,'getprop(x)')
If the returned answer is C_, there are no assumptions about the variable. (C_ means it can be any complex number.) If the returned value is anything else, there are assumptions about the variable. For example:
syms x real evalin(symengine,'getprop(x)') ans = R_ syms x clear syms z evalin(symengine,'assume(z <> 0)') evalin(symengine,'getprop(z)') ans = C_ minus {0} syms z clear evalin(symengine,'getprop(z)') ans = C_
For details about basic sets that can be returned as assumptions, enter:
doc(symengine,'solvelib::BasicSet')
Examples of the Effect of Assumptions
Assumptions can affect the answers returned by the solve function. They also can affect the results of simplifications. The only assumptions you can make using MATLAB commands are real or positive. For example, consider what transpires when solving the equation x^3 = 1:
syms x solve('x^3 = 1') ans = 1 - (3^(1/2)*i)/2 - 1/2 (3^(1/2)*i)/2 - 1/2 syms x real solve('x^3 = 1') ans = 1
However, clearing x does not change the underlying assumption that x is real:
clear x syms x solve('x^3 = 1') ans = 1
Clearing x with syms x clear clears the assumption:
syms x clear solve('x^3 = 1') ans = 1 - (3^(1/2)*i)/2 - 1/2 (3^(1/2)*i)/2 - 1/2
Using evalin or feval, you can make a variety of assumptions about an expression; see Calling Built-In MuPAD Functions from the MATLAB
Command Window on page 4-40. To clear all such assumptions, use the command syms x clear, as in this example:
evalin(symengine,'assume(a <> 0)'); evalin(symengine,'solve(a*x^2 + b*x + c = 0,x)') ans = {-(b - (b^2 - 4*a*c)^(1/2))/(2*a),. -(b + (b^2 - 4*a*c)^(1/2))/(2*a)} syms a clear evalin(symengine,'solve(a*x^2 + b*x + c = 0,x)') ans = piecewise([a -(b + (b^2 [a = 0 and [a = 0 and
new_system('my_system'); open_system('my_system')
Since the variable and its value are in the MATLAB workspace, you can use emlBlock to generate the block my_block:
emlBlock('my_system/my_block', r)
You can open and edit the block in the MATLAB Editor. To open the block, select Edit > Open Block or use the context menu.
function r = my_block(x,y) %#codegen r = sqrt(x.^2+y.^2);
Creating Simscape Equations from MuPAD Expressions
Symbolic Math Toolbox lets you integrate symbolic computations into the Simscape modeling workflow by using the results of these computations in the Simscape equation section. If you work in the MATLAB Command Window, see Generating Simscape Equations on page 3-144. If you work in the MuPAD notebook interface, you can:
Assign the MuPAD expression to a variable, copy that variable from a notebook to the MATLAB workspace, and use simscapeEquation to generate the Simscape equation in the MATLAB Command Window. Generate the Simscape equation from the MuPAD expression in a notebook. In both cases, to use the generated equation, you must manually copy the equation and paste it to the equation section of the Simscape component file. For example, follow these steps to generate a Simscape equation from the solution of the ordinary differential equation computed in the notebook interface:
1 Open a MuPAD notebook with the handle notebook_handle:
2 In this notebook, define the following equation:
s:= ode(y'(t) = y(t)^2, y(t)):
3 Decide whether you want to generate the Simscape equation in the MuPAD
notebook interface or in the MATLAB Command Window.
Generating Simscape Equations in the MuPAD Notebook Interface
To generate the Simscape equation in the same notebook, use generate::Simscape. To display generated Simscape code on screen, use the print function. To remove quotes and expand special characters like line breaks and tabs, use the printing option Unquoted:
print(Unquoted, generate::Simscape(s))
This command returns the Simscape equation that you can copy and paste to the Simscape equation section:
-y^2+y.der == 0.0;
Generating Simscape Equations in the MATLAB Command Window
To generate the Simscape equation in the MATLAB Command Window, follow these steps:
1 Use getVar to copy variable s to the MATLAB workspace:
s = getVar(notebook_handle, 's')
Variable s and its value appear in the MATLAB workspace and in the MATLAB Command Window:
s = ode(D(y)(t) - y(t)^2, y(t))
2 Use simscapeEquation to generate the Simscape equation from s:
You can copy and paste the generated equation to the Simscape equation section. Do not copy the automatically generated variable ans and the equal sign that follows it.
ans = s == (-y^2+y.der == 0.0);
Calculus (p. 5-2) Linear Algebra (p. 5-2) Simplification (p. 5-3) Solution of Equations (p. 5-3) Variable-Precision Arithmetic (p. 5-4) Arithmetic Operations (p. 5-4) Special Functions (p. 5-5) MuPAD (p. 5-5) Pedagogical and Graphical Applications (p. 5-6) Conversions (p. 5-7) Basic Operations (p. 5-8) Integral and Z-Transforms (p. 5-9) Perform calculus operations on symbolic expressions Symbolic matrix manipulation Modify or simplify symbolic data Solve symbolic expression Computing that requires exact control over numeric accuracy Perform arithmetic on symbolic expressions Specific mathematical applications Access MuPAD Provide more information with plots and calculations Convert symbolic data from one data type to another Basic operations of symbolic data Perform integral transforms and z-transforms
Diagnostics
If dsolve cannot find an analytic solution for an equation, it prints the warning:
Warning: Explicit solution could not be found.
and returns an empty sym object.
Compute symbolic eigenvalues and eigenvectors
lambda = eig(A) [V,D] = eig(A) [V,D,P] = eig(A) lambda = eig(vpa(A)) [V,D] = eig(vpa(A)) lambda = eig(A) returns a symbolic vector containing the eigenvalues of the square symbolic matrix A. [V,D] = eig(A) returns matrices V and D. The columns of V present eigenvectors of A. The diagonal matrix D contains eigenvalues. If the resulting V has the same size as A, the matrix A has a full set of linearly independent eigenvectors that satisfy A*V = V*D. [V,D,P] = eig(A) returns a vector of indices P. The length of P equals to the total number of linearly independent eigenvectors, so that A*V = V*D(P,P). lambda = eig(vpa(A)) returns numeric eigenvalues using variable-precision arithmetic. [V,D] = eig(vpa(A)) returns numeric eigenvectors using variable-precision arithmetic. If A does not have a full set of eigenvectors, the columns of V are not linearly independent.
Compute the eigenvalues for the magic square of order 5:
M = sym(magic(5)); eig(M)
ans = 65 (625/2 - (5*3145^(1/2))/2)^(1/2) ((5*3145^(1/2))/2 + 625/2)^(1/2) -(625/2 - (5*3145^(1/2))/2)^(1/2)
-((5*3145^(1/2))/2 + 625/2)^(1/2)
Compute the eigenvalues for the magic square of order 5 using variable-precision arithmetic:
M = sym(magic(5)); eig(vpa(M))
ans = 65.0 21.27676547147379553062642669797423 13.12628093070921880252564308594914 -13.126280930709218802525643085949 -21.276765471473795530626426697974
Compute the eigenvalues and eigenvectors for one of the MATLAB test matrices:
A = sym(gallery(5)) [v, lambda] = eig(A)
A [ [ [ [ [ = -9, 11, -21, 63, -252] 70, -69, 141, -421, 1684] -575, 575, -1149, 3451, -13801] 3891, -3891, 7782, -23345, 93365] 1024, -1024, 2048, -6144, 24572]
v = 0 21/256 -71/128
973/lambda = [ 0, 0, 0, [ 0, 0, 0, [ 0, 0, 0, [ 0, 0, 0, [ 0, 0, 0,
jordan | poly | svd | vpa
emlBlock
Convert symbolic expression to MATLAB Function block
emlBlock(block, f) emlBlock(block, f1, f2,.) emlBlock(block, f1, f2,., param1, value1,.) emlBlock(block, f) converts the symbolic expression f to a MATLAB
Round toward zero
fix(X) fix(X) is the matrix of the integer parts of X. fix(X) = floor(X) if X is positive and ceil(X) if X is negative.
round | ceil | floor | frac
Round symbolic matrix toward negative infinity
floor(X) floor(X) is the matrix of the greatest integers less than or equal to X. x = sym(-5/2); [fix(x) floor(x) round(x) ceil(x) frac(x)] ans = [ -2, -3, -3, -2, -1/2]
round | ceil | fix | frac
fortran
Fortran representation of symbolic expression
fortran(S) fortran(S,'file',fileName) fortran(S) returns the Fortran code equivalent to the expression S. fortran(S,'file',fileName) writes an optimized Fortran code fragment that evaluates the symbolic expression S to the file named fileName. Optimized means intermediate variables are automatically generated in order to simplify the code. MATLAB generates intermediate variables as a lowercase letter t followed by an automatically generated number, for example t32.
syms x f = taylor(log(1+x)); fortran(f)
ans = t0 = x-x**2*(1.0D0/2.0D0)+x**3*(1.0D0/3.0D0)-x**4*(1.0D0/4.0D0)+x* +*5*(1.0D0/5.0D0)
H = sym(hilb(3)); fortran(H)
ans = H(1,1) H(1,2) H(1,3) H(2,1) = = = = 1.0D0 1.0D0/2.0D0 1.0D0/3.0D0 1.0D0/2.0D0
H(2,2) H(2,3) H(3,1) H(3,2) H(3,3)
1.0D0/3.0D0 1.0D0/4.0D0 1.0D0/3.0D0 1.0D0/4.0D0 1.0D0/5.0D0
syms x z = exp(-exp(-x)); fortran(diff(z,3),'file','fortrantest');
return a file named fortrantest containing the following:
t7 = exp(-x) t8 = exp(-t7) t0 = t8*exp(x*(-2))*(-3)+t8*exp(x*(-3))+t7*t8
ccode | latex | matlabFunction | pretty
fourier
Fourier integral transform
F = fourier(f) F = fourier(f,v) F = fourier(f,u,v) F = fourier(f) is the Fourier transform of the symbolic scalar f with default independent variable x. The default return is a function of w. The Fourier transform is applied to a function of x and returns a function of w.
f = f ( x) F = F (w)
If f = f(w), fourier returns a function of t. F = F(t) By definition,
F (w) =
f ( x) eiwx dx
where x is the symbolic variable in f as determined by symvar.
F = fourier(f,v) makes F a function of the symbol v instead of the
default w.
F (v) =
f ( x) eivx dx
F = fourier(f,u,v) makes f a function of u and F a function of v instead of the default variables x and w, respectively.
f (u) eivu du
Fourier Transform MATLAB Commands
syms x; f = exp(-x^2); fourier(f)
f ( x) = e x
f ( x) eixw dx
ans = pi^(1/2)/exp(w^2/4) syms w; g = exp(-abs(w)); fourier(g)
w2 / 4
g(w) = e w F [ g ] (t) = = + t2
g(w) eitw dw
ans = 2/(v^2 + 1) syms x u; f = x*exp(-abs(x)); fourier(f,u)
f ( x) = xe x F [ f ] (u) = = 4iu (1 + u2 )2
x2 v sin v v ,
f ( x) eixu dx
ans = -(u*4*i)/(u^2 + 1)^2 syms v u; syms x real; f = exp(-x^2*abs(v))*sin(v)/v; fourier(f,v,u)
f ( x, v) = e
x real
F [ f (v) ] (u) =
f ( x, v) eivu dv
Fourier Transform
MATLAB Commands
= arc tan
+ arc tan
u+1 x2
ans = piecewise([x <> 0,. atan((u + 1)/x^2). - atan((u - 1)/x^2)])
ifourier | laplace | ztrans
Symbolic matrix element-wise fractional parts
frac(X) frac(X) is the matrix of the fractional parts of the elements: frac(X) = X - fix(X). x = sym(-5/2); [fix(x) floor(x) round(x) ceil(x) frac(x)] ans = [ -2, -3, -3, -2, -1/2]
round | ceil | floor | fix
funtool
Function calculator
funtool funtool is a visual function calculator that manipulates and displays functions of one variable. At the click of a button, for example, funtool
draws a graph representing the sum, product, difference, or ratio of two functions that you specify. funtool includes a function memory that allows you to store functions for later retrieval. At startup, funtool displays graphs of a pair of functions, f(x) = x and g(x) = 1. The graphs plot the functions over the domain [-2*pi, 2*pi]. funtool also displays a control panel that lets you save, retrieve, redefine, combine, and transform f and g.
Text Fields
The top of the control panel contains a group of editable text fields. f= g= Displays a symbolic expression representing f. Edit this field to redefine f. Displays a symbolic expression representing g. Edit this field to redefine g.
Displays the domain used to plot f and g. Edit this field to specify a different domain. Displays a constant factor used to modify f (see button descriptions in the next section). Edit this field to change the value of the constant factor.
funtool redraws f and g to reflect any changes you make to the contents of the control panels text fields.
Control Buttons
The bottom part of the control panel contains an array of buttons that transform f and perform other operations. The first row of control buttons replaces f with various transformations of f. df/dx int f simple f num f den f 1/f finv Derivative of f Integral of f Simplified form of f, if possible Numerator of f Denominator of f Reciprocal of f Inverse of f
F = F (w) f = f ( x).
If F = F(x), ifourier returns a function of t: f = f(t) By definition
f ( x) = 1 /(2 )
F (w) eiwx dw.
f = ifourier(F,u) makes f a function of u instead of the default x.
f (u) = 1 /(2 )
F (w) eiwu dw.
Here u is a scalar symbolic object.
f = ifourier(F,v,u) takes F to be a function of v and f to be a function of u instead of the default w and x, respectively.
F (v) eivu dv.
Inverse Fourier Transform MATLAB Commands
syms a w real; f = exp(-w^2/(4*a^2)); F = ifourier(f); F = simple(F)
f (w) = ew
/(4 a2 )
F 1 [ f ] ( x) = a e( ax)
f (w) eixw dw
F = abs(a)/(pi^(1/2)*exp(a^2*x^2))
g( x) = e x F 1 [ g ] ( t ) = = 2
g( x) eitx dx
syms x real; g = exp(-abs(x)); ifourier(g)
ans = 1/(pi*(t^2 + 1)) syms w t real; f = 2*exp(-abs(w)) - 1; simplify(ifourier(f,t))
1 + t2
f (w) = 2e w 1 F 1 [ f ] ( t ) = = dirac(t) +
f (w) eitw dw
ans = 2/(pi*(t^2 + 1)) - dirac(t)
(1 + t2 )
fourier | ilaplace | iztrans
ilaplace
inverse Laplace transform Inverse Laplace transform
F = ilaplace(L) F = ilaplace(L,y) F = ilaplace(L,y,x) F = ilaplace(L) computes the inverse Laplace transform of the symbolic expression L. This syntax assumes that L is a function of the variable s, and the returned value F is a function of t.
L = L(s) F = F (t)
If L = L(t), ilaplace returns a function of x. F = F(x) By definition, the inverse Laplace transform is
F (t) =
L(s) est ds,
where c is a real number selected so that all singularities of L(s) are to the left of the line s = c, i.
F = ilaplace(L,y) computes the inverse Laplace transform F as a function of y instead of the default variable t.
F ( y) =
L( y) esy ds
F = ilaplace(L,y,x) computes the inverse Laplace transform and lets you specify that F is a function of x and L is a function of y.
F ( x) =
L( y) e xy dy
Inverse Laplace Transform MATLAB Command
occurrence.
To start a new notebook and define a handle mphandle to the notebook, enter:
mphandle = mupad;
To open an existing notebook named notebook1.mn located in the current folder, and define a handle mphandle to the notebook, enter:
mphandle = mupad('notebook1.mn');
To open a notebook and jump to a particular location, create a link target at that location inside a notebook and refer to it when opening a notebook. For example, if you have the Conclusions section in notebook1.mn, create a link target named conclusions and refer to it when opening the notebook. The mupad function opens notebook1.mn and scroll it to display the Conclusions section:
mphandle = mupad('notebook1.mn#conclusions');
For information about creating link targets, see the Formatting and Exporting MuPAD Documents and Graphics section in the MuPAD Getting Started documentation.
getVar | mupadwelcome | openmn | openmu | setVar
Start MuPAD interfaces
mupadwelcome mupadwelcome opens a window that enables you to start various MuPAD interfaces:
Notebook, for performing calculations Editor, for writing programs and libraries Help, in the First Steps pane It also enables you to access recent MuPAD files or browse for files.
Creating, Opening, and Saving MuPAD Notebooks on page 4-11
Form basis for null space of matrix
Z = null(A) Z = null(A) returns a list of vectors that form the basis for the null space of a matrix A. The product A*Z is zero. size(Z, 2) is the nullity of A. If A has full rank, Z is empty.
Find the basis for the null space and the nullity of the magic square of symbolic numbers. Verify that A*Z is zero:
A = sym(magic(4)); Z = null(A) nullityOfA = size(Z, 2) A*Z
Z = -1 -1 nullityOfA = 1 ans = 0 0
Find the basis for the null space of the matrix B that has full rank:
B = sym(hilb(3))
Z = null(B)
B = [ 1, 1/2, 1/3] [ 1/2, 1/3, 1/4] [ 1/3, 1/4, 1/5] Z = [ empty sym ]
rank | rref | size | svd
numden
Numerator and denominator
[N,D] = numden(A) [N,D] = numden(A) converts each element of A to a rational form where the numerator and denominator are relatively prime polynomials with integer coefficients. A is a symbolic or a numeric matrix. N is the symbolic matrix of numerators, and D is the symbolic matrix of denominators.
Find the numerator and denominator of the symbolic number:
[n, d] = numden(sym(4/5))
n = 4 d = 5
Find the numerator and denominator of the symbolic expression:
syms x y; [n,d] = numden(x/y + y/x)
n = x^2 + y^2 d = x*y
syms a b
A = [a, 1/b] [n,d] = numden(A)
A = [a, 1/b] n = [a, 1] d = [1, b]
openmn
Open MuPAD notebook
h = openmn(file) h = openmn(file) opens the MuPAD notebook file named file, and returns a handle to the file in h. The command h = mupad(file) accomplishes the same task.
Tags
Tx9000TS TI314BS1 Aopen AK72 Speedtouch Home TXL32G20B RCD-951 Controller Motopebl WF8804DPA KDL-32S5500 Wemc10263 Attachment Maxxum7D Combisnack Soavo-900SW KM-8030 LV2785 TR-626 XR-C3100 CQ-RDP210 ATC108A VAD-WE DCR-TRV420E 20 KVA PS B23 TX-21JT1PB 880 ECO 2450-101 Quest - H Windows 7 CT-F500 ICF-C317 Command 48220 LE32A656 676 NP2500 12 Webcam 1200 TY-FB9TU TS1351 300 D250 IP4000 2150-6GS DAV-D150B 42PFL9603D 10 Frontman 15R Telstra 6100 DD9996-B SB6100 MID 2020 DEH-2000MPB EM-20 F14931FD RX-V1900 Optio H90 XR-C850RDS TE6-C NO-peep CA-R-pi 962 W2252TQ-PF Motorola I455 Vluu L100 Edirol CG-8 1000SDR XR-P160 Plafond HD9140 90 Ru 240 Argos 704 Wifi Review NV-DX110B JCM800 2203 Z Demo Vitodens ERZ36700X8 ZDI6041X 37GT-25S AVL 125 Nite-lite DSC-W170 Serie 2000 Remo Mp28 Coolpix S630 Rayline TA3 Philips HQ30 Desktop PC 245bplus MRV-F409 GR-D23E Manager Digital PRO EC System MHC-DP700 CCD-TRV3E SE6351S DC4100 CD1551B 53 IP8500 E8340-M
manuel d'instructions, Guide de l'utilisateur | Manual de instrucciones, Instrucciones de uso | Bedienungsanleitung, Bedienungsanleitung | Manual de Instruções, guia do usuário | инструкция | návod na použitie, Užívateľská príručka, návod k použití | bruksanvisningen | instrukcja, podręcznik użytkownika | kullanım kılavuzu, Kullanım | kézikönyv, használati útmutató | manuale di istruzioni, istruzioni d'uso | handleiding, gebruikershandleiding
Sitemap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

1. MATLAB Guide to Finite Elements: An Interactive Approach
2. Control Systems Engineering
3. MATLAB for Beginners: A Gentle Approach Revised Edition
4. Introduction to Applied Mathematics for Environmental Science
5. Complex Variables: A Physical Approach with Applications and MATLAB (Textbooks in Mathematics)
6. Matlab For Beginners: A Gentle Approach







