## Matlab Tutorial 6: Analysis of Functions, Interpolation, Curve Fitting, Integrals and Differential Equations

We will now try to accomplish the same thing with Matlab commands that we did with the interactive basic fitting in the figure window.

>> x=-10:0.1:10; >> plot(x,3*x.^3+2*x.^2-2*x+4), title('p(x)=3*x^3+2*x^2-2*x+4') >> xlabel('x'), grid |

The result of the commands can be seen in the figure below. The higher the order of the polynomial, the higher the oscillation between the data points. Notice the huge peaks between values 1 and 2 and 9 and 10. You should have been alarmed if this had been real data and your mission was to find an interpolation function that nicely fits the measurements. It is definitely not common sense to look for a ninth-order polynomial for a data set of 10 points.

### Integrals

Matlab can solve integrals and differential equations with numerical methods. Later on in this course we will show how integrals and differential equations can be solved in Symbolic Math toolbox. Numerical integration in Matlab is performed by the command quad. This stands for numerical quadrature. We can solve integrals only with defined limits. This can be done for single, double and triple integrals. To solve generalized integrals or integrals expressed with symbols, we must use the Symbolic Math toolbox. Let us exemplify with some numerical examples.

### Example 1: Single integrals

Decide the integral below.

First we put the integrand in a function file: integrand.m.

>> p=[3 2 -2 4] % Represents the coefficients in p(x) |

Write!

>> polyval(p,6), polyval(p,7), polyval(p, -5) ans= 712 , ans = 1117 , ans = -311 |

Matlab gives:

>> C=1 % C is a integration constant. >> Pder=polyder(p), Pint=polyint(p,C) Pder = 9 4 -2 Pint = 0.7500 0.6667 -1.0000 4.0000 1.0000 |

It seems reasonable enough, if we plot the function. We can also use quad with a fourth argument, namely the tolerance for the calculation. This decides when the numerical integration will stop. The two following examples can be hard to understand, if one lacks the background of multivariable calculus.

### Example 2: Double integrals

Calculate the double integral below.

Make as in the previous example a functionwhere we put the primitive function. We name the m-file as integrand2.m

>> q=[1 0] |

Let us now call the function dblquad instead of it.

>> conv(p,q) % Performs a polynomial multiplication of p and q. ans= 3 2 -2 4 0 |

and the answer is:

roots(p) % Gives a vector with zeros to the polynomial p(x). ans = -1.6022 0.4678 + 0.7832i 0.4678 - 0.7832i |

We will now take a look of the integrated area.

[ t,n,the_rest]=residue(q,p) % There are 3 output arguments from residue. t = -0.1090 0.0545 - 0.0687i 0.0545 + 0.0687i n = -1.6022 0.4678 + 0.7832i 0.4678 - 0.7832i the_rest = [] |

The last three lines achieves Figure below.

Matlab can also perform triple integrals. This example can be found by help triplequad.

### Example 3: Triple integrals

A function f = y*sin(x)+z*cos(x) is integrated for the intervals 0 < x < pi, 0 < y < 1 and -1 < z < 1. This is achieved by:

First we create a function file integrnd.m.

% The function func.m created by MatlabCorner.Com function f=func(x) f=3*sin(x)-2+cos(x.^2); |

We refer to this m-file by using its handle. In the matlab command window we simply write:

>> x=-10:0.1:10; >> plot(x,func(x)), grid on % Note that: fplot(@func,[-10,10]) >> title( 'func(x)') % works equally well. |