Sunday, March 13, 2016

Integration and Differentiation in MATLAB

Differential calculus with Matlab.

The diff command can be used to compute derivatives. You must always remember to declare the variable (t in the example below) as a symbolic variable first (we do that with the command sym below.)

>> t=sym('t','real')
>> f=t*sin(3*t^2)
>> diff(f)

You can also differentiate vector-valued functions. For instance, if
then you can compute the derivative of the above equation r'(t) as follow

>> r=[t, 0, t*cos(t^2)]
>> v=diff(r)

To compute r'(2), you can use subs(v,2). The speed of the object at time t is given by the norm of r'(t) However, the command norm does not work with symbolic variable. So to compute ||r'(t)|| we use sqrt(dot(v,v)).

Integral calculus with Matlab. 

Integrals are computed with the command int. For instance, int(r) computes an inde nite integral of ~r. The command int(r,0,1) computes the defi nite integral over the interval [0; 1].
Note that int attempts to fi nd an exact formula. If that fails (and it will fail for complicated functions), you can use the command double to get a numerical result. Try to the following example:

>> t=sym('t','real')
>> g=exp(cos(t))
>> int(g,0,1)
>> double(int(g,0,1))

You can also compute the integral of vector valued functions. For instance, here is how to compute
>> F=[exp(t),t^2,cos(t)]
>> int(F,0,1)

Plotting Surfaces

The graph z = f(x; y) can be drawn with ezmesh. For instance, to draw the surface
you would type

>> ezmesh('exp(x)-y*sin(2*x)',[-5 2 -2 2])

You can increase the resolution of the picture by increasing the number of grid points. For instance, try

>> ezmesh('exp(x)-y*sin(2*x)',[-5 2 -2 2],200)

Plotting parametric curves and surfaces:

The following equation is a parametric equation for a curve, it will be used in the following steps to create vase surface, 
to plot that curve the following matlab code is used

r = 2*pi*(0:24)/24;
s = 2*pi*(0:24)/24;
[t phi] = meshgrid(r,s);
f = '0*t';
g = '10*sqrt(t) + 20*sin(t)';
h = '30*t';
F = vectorize(f);
G = vectorize(g);
H = vectorize(h);
x = eval(F);
y = eval(G);
z = eval(H);
surf(x,y,z)

The resulting curve is used to make vase surface by rotating the above curve around z 360 degrees. 
The curve can be rotated by the following equation
the right part of the equation is the equation of the resulting vase surface in terms of t, u. The surface can be plotted with the following matlab command

r = 2*pi*(0:24)/24;
s = 2*pi*(0:24)/24;
[u v] = meshgrid(r,s);
f = '-(3*sqrt(v) + 5*sin(v))*sin(u)';
g = '(3*sqrt(v) + 5*sin(v))*cos(u)';
h = '4*v';
F = vectorize(f);
G = vectorize(g);
H = vectorize(h);
x = eval(F);
y = eval(G);
z = eval(H);
surf(x,y,z)

The resulting surface is shown in the following figure





No comments:

Post a Comment