You are on page 1of 10

Expt - 3 Partial derivatives, Jacobian and graphs

Agenda

Following will be covered with appropriate graphs

1. Symbolic maths
2. Differentiaion
3. Partial differentiaion
4. Plotting differentiation
5. Jacobian
6. Plotting symbolic using implicit function

Functions introduced in this lab

1. syms (not really a function!)


2. diff() for differentiation and partial differentiaion
3. matlabFunction() Convert symbolic expression to function handle or file
4. jacobian() Jacobian matrix
5. fimplicit () Plot implicit function
6. fplot() ; Plot expression or function

3.1 Symbolic maths


As the name indicates, symbolic maths allows to use symbols in the mathematical equations. One can assign
values to the symbols later on.

Symbolic Maths lets user to analytically perform differentiation, integration, simplification, transforms, and
equation solving. User can perform dimensional computations and convert between units. Computations can be
performed either analytically or using variable-precision arithmetic, with the results displayed in mathematical
typeset.

Symbolic maths in Matlab uses syms as the definition.

Syms creates symbolic scalar variables & functions, and matrix variables & functions

In MATLAB, syms is used as a shortcut to the inbuilt function sym. This function can be used to create symbolic
variables. The Symbolic variables used in MATLAB are not constants like the regular variables; we do not
assign values to them. These are used to solve various expressions with the help of functions available in
Symbolic Math Toolbox. The syms function creates a symbolic object that is automatically assigned to a
MATLAB variable with the same name. (https://www.educba.com/matlab-syms/)

We can allot values for the symbolic variables as and when needed. Let

3.1.1 Let us see an example - defining a symbolic variable

syms a ; % define a symbolic variable


syms x [2 2] ; %define a symbolic array

1
a % display a

a = 

x % display x

x = 

We see that system displays nothing for a and x as a matrix with values x1_1 and so on (but no value is
attached to them). Values to symbolic variables can be assigned later on

3.1.2 Passing values to variables

% Pythagoras theorem using symbolic variables


syms f(x,y) % define the symbolic variables
f(x,y) = sqrt (x^2 + y^2) ; % assign Pythagoros theorem to the symbolic function
a = f(4,6) % call the function with specific value

a = 

vpa(a) % display the value of a

ans = 

That is just a brief introduction to symbolic mathematics. Using this information, we need to compute
differentiation, partial differentiation and Jacobian operators. They are considered in next parts

3.2 Differentiation

For differentiation, we will use syms and define a function. Then we shall (symbolically) differentiate the
function

clear; clear all % clear the variables declared earlier (if any)
syms x ; % define a symbol x
f= 5*x^3 + 4*x^2 + 3*x % Define a function

f = 

fx=diff(f,x) ; %generate regular differentiation


disp(fx) % display the result

% verify the differentiation of the function

Try with other equations

2
What happens if the last variable is 3*x*y?

clear; clear all % clear the variables declared earlier (if any)
syms x y; % define a symbol x
f= 5*x^3 + 4*x^2 + 3*x*y % Define a function

f = 

fx=diff(f,x) ; %generate regular differentiation


disp(fx) % display the result

% verify the differentiation of the function

3.3 Partial differentiation

In partial differentiation, we have two variables. We differentiate with each variable, treating the one variable(s)
as constant

clear; clear all


syms x y ;
f=x^3 + x^2 *y^3 -2*y^2 % create a function in x and y

f = 

fs=diff(f,x) % Differentiate with respect to x

fs = 

fy = diff(f,y) % differentiate with respect to y

fy = 

Try with other equations

To prove for u = exp(x)(xcos(y) − ysin(y)). (partial derivatives)

clear; clc
syms x y;
u = exp (x) * (x * cos(y) - y * sin(y))

u = 

dux = diff (u , x ); % Differentate u w . r . t x


duy = diff (u , y );
duxy = diff ( dux , y ) %second derivative

3
duxy = 

duyx = diff ( duy , x )

duyx = 

if duxy == duyx % Check if they are same


display('proved')
else
display('something wrong!!')
end

proved

To prove u = ex(x cos(y) − y sin(y)) then uxx + uyy = 0.

clear; clc
syms x y ; % define symbolic variables
u = exp (x)*(x*cos(y) - y*sin(y)); %define function
dux = diff (u , x ); % first (partial) derivative
duy = diff (u , y );
uxx = diff ( dux , x ); % second (partial) derivative of u w . r . t x
uyy = diff ( duy , y );
w=uxx+uyy % sum of both partial functions

w = 

w1=simplify(w) % simplyfy the results

w1 = 

vpa(w1) %display the symbolic variabls

ans = 

3.4 Plotting differentiation

Generate a sine wave and differentiate it. Plot both the curves

clear; clear all


syms x y ;
f=sin(x) % create a function in x and y

f = 

fs=diff(f,x) % Differentiate with respect to x

fs = 

4
a=matlabFunction(f) % convert the symbolic value to plottable value

a = function_handle with value:


@(x)sin(x)

b= matlabFunction(fs)

b = function_handle with value:


@(x)cos(x)

fplot(a); hold on; fplot(b) %plot both the functions


hold off % needed for next programs

Let us differentiate twice (2nd order differentiation) and plot original, first-order and second-order differentiaions

syms t;
x = t*exp(-3*t)+0.25*exp(-3*t) % define a function

x = 

xdot = diff(x,t,1) % first order differentiaion

xdot = 

5
xddot = diff(x,t,2) % second order differentiation

xddot = 

x1=matlabFunction(x) % Convert to plottable function

x1 = function_handle with value:


@(t)exp(t.*-3.0)./4.0+t.*exp(t.*-3.0)

subplot(311);
fplot(x1,[0 3]) % plot the original signal
subplot(312);
xdot1=matlabFunction(xdot)

xdot1 = function_handle with value:


@(t)exp(t.*-3.0)./4.0-t.*exp(t.*-3.0).*3.0

fplot(xdot1,[0 3]) % plot the first order differentiation


subplot(313);
xddot1 = matlabFunction(xddot)

xddot1 = function_handle with value:


@(t)exp(t.*-3.0).*(-1.5e+1./4.0)+t.*exp(t.*-3.0).*9.0

fplot(xddot1,[0 3]) % plot the second order differentiation

6
3.5 Jacobian

If x = g(u, v) and y = h(u, v) are transformation of the plane. Then the Jacobian of this transformation is

If u = xy/z, v = yz/x, w = zx/y then prove that J = 4.

clear;clc % clear variables, figures and command window


syms x y z
u=x*y/z ; %define functions
v =y*z/x ;
w= z*x/y ;
J = jacobian([u,v,w],[x,y,z]) % generate jacobian matrix

J = 

7
det(J) % find the determinant (verify it is 4)

ans = 

If u = x + 3y2 − z3, v = 4x2yz, w = 2z2 − xy then prove that at (1, −1, 0), J = 20

% similar to the previous program


syms x y z
u=x+3*y^2 ;
v = 4*x^2 * y *z ;
w = 2*z^2 - x*y ;
J = jacobian([u,v,w],[x,y,z])

J = 

J1= subs (J,[x,y,z,],[1,-1,0]) % Jacobian matrix with x=1, y = -1 and z = 0

J1 = 

det(J1) % Ensure that result is 20 (value we need to prove)

ans = 

3.6. Plotting symbolic functions

6.1 Symbolic functions can be plotted using implicit function

figure()
f1 = @(x,y) x.^2 + y.^2 - 1; %define function for a circcle
fimplicit(f1,'r') % plot it with red color

8
% you may want to try with axis equal

6.2 VTU code

Strophoid: y2(a − x) = x2(a + x), a > 0

syms x y ;
f2 = @(x,y) y.^2 * (2-x)- x.^2 * (2+x) ;
figure()
fimplicit(f2, [-4 4 -5 5]);

Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize
your function to return an output with the same size and shape as the input arguments.

grid on; grid minor % plot the code with grid

9
narasimha.kaulgud@nie.ac.in

10

You might also like