# A Guide to MATLAB for Beginners and Experienced Users - Brian R.H.

**Download**(direct link)

**:**

**24**> 25 26 27 28 29 30 .. 91 >> Next

>> syms x; int(exp(-x"4), 0, 1)

Warning: Explicit integral could not be found. > In /data/matlabr12/toolbox/symbolic/@sym/int.m at line 58

ans =

int(exp(-x^4),x = 0 .. 1) >> quadl(vectorize(exp(-x"4)), 0, 1)

ans =

0.8448

^ The commands quad, quad8, and quadl will not accept Inf or - Inf as a limit of integration (though int will). The best way to handle a numerical improper integral over an infinite interval is to evaluate it over a very large interval.Doing Calculus with MATLAB 63

? You have another option. If you type double(int( )), then Maple's numerical integration routine will evaluate the integral — even over an infinite range.

MATLAB can also do multiple integrals. The following command computes the double integral

ã Ï r sin x

/ / (x2 + y2)dydx :

00

>> syms x y; int(int(x"2 + y"1, y, 0, sin(x)), 0, pi)

ans = pi^2-32/9

Note that MATLAB presumes that the variable of integration in int is x unless you prescribe otherwise. Note also that the order of integration is as in calculus, from the "inside out". Finally, we observe that there is a numerical double integral command dblquad, whose properties and use we will allow you to discover from the online help.

Limits

You can use limit to compute right- and left-handed limits and limits at infinity. For example, here is limsin(x)/x:

>> syms x; limit(sin(x)/x, x, 0)

ans = 1

To compute one-sided limits, use the 'right' and 'left' options. For example,

>> limit(abs(x)/x, x, 0, 'left')

ans = -1

Limits at infinity can be computed using the symbol Inf:

>> limit((x"4 + x"2 - 3)/(3*x"4 - log(x)), x, Inf)

ans = 1/364

Chapter 4: Beyond the Basics

Sums and Products

Finite numerical sums and products can be computed easily using the vector capabilities of MATLAB and the commands sum and prod. For example,

>> X = 1:7; >> sum(X)

28

>> prod(X)

ans =

5040

You can do finite and infinite symbolic sums using the command symsum. To illustrate, here is the telescoping sum

>> symsum(1/n"2, 1, Inf)

ans = 1/6*pi^2

Another familiar example is the sum of the infinite geometric series: >> syms a k; symsum(a"k, 0, Inf)

ans

>> syms k n; symsum(1/k - 1/(k + 1), 1, n)

ans

1/(n+1)+1

And here is the well-known infinite sum

ans =

-1/(a-1)

Note, however, that the answer is only valid for |a| < 1.Default Variables 65

Taylor Series

You can use taylor to generate Taylor polynomial expansions of a specified order at a specified point. For example, to generate the Taylor polynomial up to order 10 at 0 of the function sin x, we enter

>> syms x; taylor(sin(x), x, 10)

ans =

õ-1/6*õë3+1/12 0*õë5-1/504 0*õë7+1/3 6288 0*õë9

You can compute a Taylor polynomial at a point other than the origin. For example,

>> taylor(exp(x), 4, 2)

ans =

åõð(2)+åõð(2)*(õ-2)+1/2*åõð(2)*(õ-2)ë2+1/6*åõð(2)*(õ-2)ë3

computes a Taylor polynomial of ex centered at the point x = 2.

The command taylor can also compute Taylor expansions at infinity:

>> taylor(exp(1/x"2), 6, Inf)

ans =

1+1/õë2+1/2/õë4

Default Variables

You can use any letters to denote variables in functions — either MATLAB's or the ones you define. For example, there is nothing special about the use of t in the following, any letter will do as well:

>> syms t; diff(sin(t"2))

ans =

2*cos(t^2)*t

However, if there are multiple variables in an expression and you employ a MATLAB command that does not make explicit reference to one of them, then either you must make the reference explicit or MATLAB will use a built-in hierarchy to decide which variable is the "one in play". For example,66

Chapter 4: Beyond the Basics

solve('x + y = 3') solves for x, not y. If you want to solve for y in this example, you need to enter solve('x + y = 3', 'y'). MATLAB's default variable for solve is x. If there is no x in the equation(s), MATLAB looks for the letter nearest to x in alphabetical order (where y takes precedence over w, but w takes precedence over z, etc). Similarly for diff, int, and many other symbolic commands. Thus syms w z; diff w*z yields z as an answer. On occasion MATLAB assigns a different primary default variable — for example, the default independent variable for MATLAB's symbolic ODE solver dsolve is t. This is mentioned clearly in the online help for dsolve. If you have doubt about the default variables for any MATLAB command, you should check the online help.Chapter 5

MATLAB Graphics

In this chapter we describe more of MATLAB's graphics commands and the most common ways of manipulating and customizing them. You can get a list of MATLAB graphics commands by typing help graphics (for general graphics commands), help graph2d (for two-dimensional graphing), help graph3d (for three-dimensional graphing), or help specgraph (for specialized graphing commands).

We have already discussed the commands plot and ezplot in Chapter 2. We will begin this chapter by discussing more uses of these commands, as well as the other most commonly used plotting commands in two and three dimensions. Then we will discuss some techniques for customizing and manipulating graphics.

**24**> 25 26 27 28 29 30 .. 91 >> Next