필터 지우기
필터 지우기

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

조회 수: 216 (최근 30일)
NoorKh
NoorKh 2019년 12월 13일
댓글: IBTASHAM IMAM 2024년 6월 17일
How can I write code to calculate the partial derivatives a given function of two variable ?

답변 (2개)

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

IBTASHAM IMAM
IBTASHAM IMAM 2024년 6월 17일
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
IBTASHAM IMAM
IBTASHAM IMAM 2024년 6월 17일
% 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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by