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

40 ビュー (過去 30 日間)
NoorKh
NoorKh 2019 年 12 月 13 日
移動済み: Dyuman Joshi 2025 年 6 月 28 日
How can I write code to calculate the partial derivatives a given function of two variable ?
  2 件のコメント
IBTASHAM IMAM
IBTASHAM IMAM 2024 年 6 月 17 日
移動済み: Dyuman Joshi 2025 年 6 月 28 日
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).
IBTASHAM IMAM
IBTASHAM IMAM 2024 年 6 月 17 日
移動済み: Dyuman Joshi 2025 年 6 月 28 日
% 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);

サインインしてコメントする。

採用された回答

Dyuman Joshi
Dyuman Joshi 2019 年 12 月 14 日
編集済み: Dyuman Joshi 2023 年 8 月 12 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by