Effacer les filtres
Effacer les filtres

how to calculate the partial derivatives for a given function of two variable ?

80 vues (au cours des 30 derniers jours)
NoorKh
NoorKh le 13 Déc 2019
Commenté : IBTASHAM IMAM le 17 Juin 2024
How can I write code to calculate the partial derivatives a given function of two variable ?

Réponses (2)

Dyuman Joshi
Dyuman Joshi le 14 Déc 2019
Modifié(e) : Dyuman Joshi le 12 Août 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)
fx(x, y) = 
fy = diff(f,y)
fy(x, y) = 
You can also find the value of parial derivatives by substituting the value -
fx(1,0)
ans = 
2

IBTASHAM IMAM
IBTASHAM IMAM le 17 Juin 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 commentaire
IBTASHAM IMAM
IBTASHAM IMAM le 17 Juin 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);

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by