Problem in evaluating the value of the derivative of multivariable function

1 view (last 30 days)
I'm triyng to make a function that uses the Newton - Raphson method to solve a system of 2 equation
function [x_final, y_final, f1_final, f2_final, iter, errs, arrs] = Sys_equa_newton(f1, f2, x0, y0, err, max_iter)
%newton_sys - Description
%
% Syntax: y = newton_sys(input)
%
% Long description
% f1: first funtion of 2 variables
% f2: second funtion of 2 variables
% x0: initial value of x
% y0: initial value of y
% err: threshold value of error to stop
% max_iter: maximum iteration to stop
cur_err = 1;
iter = 0;
syms x y;
df1_x = matlabFunction(diff(f1, x));
df1_y = matlabFunction(diff(f1, y));
df2_x = matlabFunction(diff(f2, x));
df2_y = matlabFunction(diff(f2, y));
x = x0;
y = y0;
X_n = f1(x, y) * (df2_y(x, y)) - f2(x, y) * (df1_y(x, y));
Y_n = f2(x, y) * (df1_x(x, y)) - f1(x, y) * (df2_x(x, y));
Deno = df1_x(x, y) * df2_y(x, y) - df1_y(x, y) * df2_x(x, y);
arrs = zeros(2,max_iter);
errs = zeros(2,max_iter);
while ((cur_err > err) && (iter <= max_iter))
x_old = x;
x = x_old - X_n / Deno;
y_old = y;
y = y_old - Y_n / Deno;
err_x = abs(x - x_old) / x;
err_y = abs(y - y_old) / y;
arrs(:,iter+1) = [x; y];
errs(:,iter+1) = [err_x; err_y];
iter = iter+1;
cur_err = err_x + err_y;
end
x_final = x;
y_final = y;
f1_final = f1(x_final,y_final);
f2_final = f2(x_final,y_final);
end
But if I enter a function that only has the first power of x or y or both x and y are in first power, when i calculate the derivatrive (df1_x for example) then the new function only have 1 argument or 0 argument if both x and y are in first power.
So is there anyway to solve this problem, I'm trying to make a general function that can work with all 2 system of equation

Accepted Answer

Walter Roberson
Walter Roberson on 24 Sep 2022
matlabFunction(diff(f1, x), 'vars', [x, y] )

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!