Unrecognized function or variable 'x'.
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am having a difficult time understanding how to define "x" in this scenario.
Here's the Problem:

Here's my code:
%%%%% Problem 3.13 %%%%%
xi = 1; % given initial x value
yi = 1; % given initial y value
e = 0.001; % error/tolerance
i_max = 5; % max number of iterations
f1 = @(x,y) -2*x^3 + 3*y^2 + 42; % function 1
f2 = @(x,y) 5*x^2 + 3*y^3 - 69; % function 2
d_f1x = diff(f1,x); %partial derivative of f1 wrt x
d_f1y = diff(f1,y); %partial derivative of f1 wrt y
d_f2x = diff(f2,x); %partial derivative of f2 wrt x
d_f2y = diff(f2,y); %partial derivative of f2 wrt y
%Jacobian
J = @(x,y) -60*x*y - 54*x^2*y^2;
for i = 1:i_max
Ji = J(xi, yi);
del_x = (-f1(xi,yi)*d_f2y(yi) + f2(xi,yi)*d_f1y(yi))/Ji; %delta x
del_y = (-f2(xi,yi)*d_f1x(xi) + f1(xi,yi)*d_f2x(yi))/Ji; %delta y
x1 = xi + del_x;
y1 = yi + del_y;
e_x = abs((x1-xi)/xi);
e_y = abs((y1-yi)/yi);
if e_x < e & e_y < e
break
else
xi = x1;
yi = y1;
end
end
%x_Newton = []
%y_Newton = []
I get the error :
Unrecognized function or variable 'x'.
Error in solution (line 51)
d_f1x = diff(f1,x); %partial derivative of f1 wrt x
0 commentaires
Réponses (1)
Alan Stevens
le 16 Sep 2020
To avoid your error message simply specify the derivatives directly (they are easy to obtain from polynomials). With both initial guesses at 1 the system diverges (perhaps that's why you are told only to do 5 iterations!). Setting y = -1 allows the system to converge). Try the following (modify as you see fit):
%%%%% Problem 3.13 %%%%%
xi = 1; % given initial x value
yi = -1; % given initial y value (diverges if both x and y = 1)
i_max = 5; % max number of iterations (use 7 iterations to get f1, f2 = 0)
f1 = @(x,y) -2*x^3 + 3*y^2 + 42; % function 1
f2 = @(x,y) 5*x^2 + 3*y^3 - 69; % function 2
d_f1x = @(x) -6*x.^2; %partial derivative of f1 wrt x
d_f1y = @(y) 6*y; %partial derivative of f1 wrt y
d_f2x = @(x) 10*x; %partial derivative of f2 wrt x
d_f2y = @(y) 9*y.^2; %partial derivative of f2 wrt y
%Jacobian
J = @(x,y) [d_f1x(x) d_f1y(y);
d_f2x(x) d_f2y(y)];
F = @(x,y) [f1(x,y);
f2(x,y)];
for i = 1:i_max
Ji = J(xi, yi);
Fi = F(xi, yi);
del = Ji\Fi;
xy = [xi;yi] - del;
xi = xy(1);
yi = xy(2);
end
disp([xi yi])
disp([f1(xi,yi) f2(xi,yi)])
0 commentaires
Voir également
Catégories
En savoir plus sur Elements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!