how to calculate the partial derivatives for a given function of two variable ?
216 views (last 30 days)
Show older comments
How can I write code to calculate the partial derivatives a given function of two variable ?
0 Comments
Answers (2)
Dyuman Joshi
on 14 Dec 2019
Edited: Dyuman Joshi
on 12 Aug 2023
Hello, You can use diff function operator to obtain partial derivatives as follows:
1- Define the function using symbolic variables
%Random function for example
syms x y;
f(x,y) = x^2 + y^2 + x*y;
2-use diff with respect to the variable you want to differentiate.
fx = diff(f,x)
fy = diff(f,y)
You can also find the value of parial derivatives by substituting the value -
fx(1,0)
0 Comments
IBTASHAM IMAM
on 17 Jun 2024
Using MATLAB, find the partial derivatives of F(x,y)=x^3+y^3+6xy-1 with respect to y at the point (1,1).
1 Comment
IBTASHAM IMAM
on 17 Jun 2024
% Symbolic variables
syms x y lambda;
% Define the function and constraint
f = 3*x + 4*y;
g = x^2 + y^2 - 1;
% Define the Lagrange function
L = f + lambda*g;
% Take partial derivatives of L with respect to x, y, and lambda
Lx = diff(L, x);
Ly = diff(L, y);
Ll = diff(L, lambda);
% Solve the system of equations for critical points
eqn1 = Lx == 0;
eqn2 = Ly == 0;
eqn3 = Ll == 0;
solutions = solve([eqn1, eqn2, eqn3], [x, y, lambda]);
% Extract the solutions
x_sol = solutions.x;
y_sol = solutions.y;
lambda_sol = solutions.lambda;
% Substitute critical points back into the original constraint
g_val = subs(g, {x, y}, {x_sol, y_sol});
% Check for valid solutions (points on the circle)
valid_solutions = abs(g_val) < 1e-10; % Allow for numerical precision issues
% Extract valid critical points and function values
x_valid = double(x_sol(valid_solutions));
y_valid = double(y_sol(valid_solutions));
f_val = double(subs(f, {x, y}, {x_valid, y_valid}));
% Find the maximum and minimum values of f
f_max = max(f_val);
f_min = min(f_val);
% Display results
disp('Maximum value of f(x, y):');
disp(f_max);
disp('Minimum value of f(x, y):');
disp(f_min);
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!