How to Calculate Higher-Order Derivatives

There exists a fifth-order implicit function (curve) such that f(x,y)=a (a is a constant)
For example, x^5+x^4*y^1+x^3*y^2+x^2*y^3+x^1*y^4+y^5=10
There exists a 3-dimensional surface such that g(x,y)=z.
For example, z=x^5+xy+y^5
Next, find the cubic curve g(x,y)=z along f(x,y)=a
Then, on the curve z, we want to find (x,y) where dz/dx=0 and dz/dy=0.

20 Comments

Hi 高木 範明,

Is this what you are looking for.

% Define the implicit function f(x,y) = a

syms x y a

f = x^5 + x^4*y + x^3*y^2 + x^2*y^3 + x*y^4 + y^5 - a;

% Define the 3-dimensional surface g(x,y) = z

g = x^5 + x*y + y^5;

% Find the intersection curve of f(x,y) = a and g(x,y) = z

curve = fimplicit3(f - 10, [0 10 0 10 0 10]);

hold on

% Find points where dz/dx = 0 and dz/dy = 0 on the curve

[x_sol, y_sol] = solve([diff(g, x) == 0, diff(g, y) == 0], [x, y]);

% Plot the final result

scatter(x_sol, y_sol, 'filled', 'r');

xlabel('x');

ylabel('y');

title('Intersection Points on the Curve');

legend('Intersection Curve', 'Intersection Points', 'Location', 'Best');

Note: I will ignore the warning message that appears after executing code for now.

Please see attached plot.

Torsten
Torsten on 31 Jul 2024
Edited: Torsten on 31 Jul 2024
Could you explain what you want for a simple case, e.g.
f(x,y) = x^2+y^2-10
and
z = g(x,y) = x + y
?
Maybe you try to
minimize/maximize g(x,y)
under the constraint
f(x,y) = 0
This works with the method of Lagrange.
@Torsten,
Glad you brought this up, “ This works with the method of Lagrange”, since Lagrange multiplier helps solve such constrained optimization problems, but you have to introduce a new variable (lambda) to incorporate the constraint into the objective function and by setting up the Lagrangian function will help find critical points that will satisfy both the original function and the constraint and solving the resulting system of equations will provide the optimal values of x and y that will maximize or minimize g(x,y) while meeting the constraint f(x,y) = 0. Hope, this is what you have in mind.
To: Umar - san
I have made some changes to your code to match the results I want.
The coordinates I want are the points of intersection near (1.1, 1.1, 4.5).
I cannot get this point right.
% Define the implicit function f(x,y) = a
syms x y z a % add z
f = x^5 + x^4*y + x^3*y^2 + x^2*y^3 + x*y^4 + y^5; % remove a
% Define the 3-dimensional surface g(x,y) = z
g = x^5 + x*y + y^5;
% Find the intersection curve of f(x,y) = a and g(x,y) = z
curve = fimplicit3(f - 10, [0 10 0 10 0 10]);
hold on
curve2 = fimplicit3(g-z, [0 10 0 10 0 10]); % add
hold on % add
% Find points where dz/dx = 0 and dz/dy = 0 on the curve
[x_sol, y_sol] = solve([diff(g, x) == 0, diff(g, y) == 0], [x, y]);
% Plot the final result
scatter(x_sol, y_sol, 'filled', 'r');
Warning: Using only the real component of complex data.
xlabel('x');
ylabel('y');
title('Intersection Points on the Curve');
legend('Intersection Curve', 'Intersection Points', 'Location', 'Best');
P.S:
I don't know how to calculate the equation for the curve where f(x,y)=-10 and g(x,y,z)==0 intersect.
Sorry. There was a mistake. f(x,y)=-10 is wrong, f(x,y)=10 is correct.
Hi @ 高木 範明-San,
By correcting the equation for f(x, y) to f(x, y) = 10 and ensuring the correct representation of the functions, you can now run the code to find the intersection points of the curve where f(x, y) = 10 and g(x, y, z) = 0 intersect near the desired coordinates. This adjustment should help you obtain the accurate intersection points you are looking for. Just to give you heads up, it is generating some warnings but you can figure it out how to fix them, here is updated code.
syms x y z
f = x^5 + x^4*y + x^3*y^2 + x^2*y^3 + x*y^4 + y^5 - 10;
g = x^5 + x*y + y^5;
curve = fimplicit3(f, [0 10 0 10 0 10]);
hold on
curve2 = fimplicit3(g, [0 10 0 10 0 10]);
[x_sol, y_sol] = solve([diff(g, x) == 0, diff(g, y) == 0], [x, y]);
scatter(double(x_sol), double(y_sol), 'filled', 'r');
Warning: Using only the real component of complex data.
xlabel('x');
ylabel('y');
zlabel('z'); % Add z-axis label for 3D plot
title('Intersection Points on the Curve');
legend('Intersection Curve', 'Intersection Points', 'Location', 'Best');
grid on; % Enable grid display
Warning: Error creating or updating TriangleStrip
Error in value of property StripData
Array is wrong shape or size
Hope this helps resolve your problem. Please let me know if you have any further questions.
I don't know how to calculate the equation for the curve where f(x,y)=10 and g(x,y,z)==0 intersect.
The curve is given implicitly by the solutions for the system of equations f(x,y) - 10 = 0 and g(x,y,z) = 0. It's usual that no explicit equation can be deduced.
@Torsten,
It is because these curves do not have a straightforward algebraic representation like explicit curves and to point out the complexity arises from the intertwined nature of variables within the equations, making it challenging to isolate a single variable explicitly. That is a good point, thanks for bringing this up.
To: Torsten - san
Finding a solution to the system of equations f(x,y) - 10 = 0 and g(x,y,z) = 0 in the form y=k(x) yields five solution equations because it is a fifth-order equation. Therefore, differentiation must also be performed on the five equations, which is very tedious. Therefore, I am thinking that if we can obtain the solution as an implicit function, it will become one equation. I would like to know how to calculate it.
To: Umar - san
Thanks for your reply.
I have changed the formula slightly.
I would like to know the coordinates of the white point on the curve at the intersection.
Please check again.
syms x y z
f = x^5 + x^4*y + x^3*y^2 + x^2*y^3 + x*y^4 + y^5 - 10;
g = x^5 + x*y + y^5;
curve = fimplicit3(f, [0 10 0 10 0 10]);
hold on
curve2 = fimplicit3(g-z, [0 10 0 10 0 10]); % g -> g-z
[x_sol, y_sol] = solve([diff(g, x) == 0, diff(g, y) == 0], [x, y]);
scatter(double(x_sol), double(y_sol), 'filled', 'r');
xlabel('x');
ylabel('y');
zlabel('z'); % Add z-axis label for 3D plot
title('Intersection Points on the Curve');
legend('Intersection Curve', 'Intersection Points', 'Location', 'Best');
grid on; % Enable grid display
Hi 高木 範明 -San,
My analysis of your code and significance of knowing the coordinates of the white point
Basically your code is visually trying to represent the intersection points between two 3D surfaces and provide a clear visualization of where these surfaces intersect. By leveraging symbolic computations and plotting functions in your code, you are trying to performing a comprehensive analysis of the intersection geometry in a 3D space. Also, your purpose of knowing the coordinates of the white point at the intersection will provide crucial information about where the two functions intersect in the 3D space since these intersection points are significant because they represent solutions to the system of equations formed by the functions described above by you and by identifying these points, you can analyze the behavior of the functions at their intersection, determine common solutions, and understand the relationship between the functions.
Analysis of your code
Addressing your query regarding, “Please check again.”, the code provided by you below shows error
syms x y z
f = x^5 + x^4*y + x^3*y^2 + x^2*y^3 + x*y^4 + y^5 - 10;
g = x^5 + x*y + y^5;
curve = fimplicit3(f, [0 10 0 10 0 10]);
hold on
curve2 = fimplicit3(g-z, [0 10 0 10 0 10]); % g -> g-z
x_sol, y_sol] = solve([diff(g, x) == 0, diff(g, y) == 0], [x, y]); ↑
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Now, addressing your query about, “I would like to know the coordinates of the white point on the curve at the intersection”
If I interpreted your statement correctly, this is probably what you meant
% Solve the equations to obtain x_sol and y_sol
[x_sol, y_sol] = solve([diff(g, x) == 0, diff(g, y) == 0], [x, y]);
% Extract the real components of x_sol and y_sol
x_sol_real = double(x_sol);
y_sol_real = double(y_sol);
% Assuming x_sol_real and y_sol_real are complex data
% Extracting the real component of the data
x_sol_real_real = real(x_sol_real);
y_sol_real_real = real(y_sol_real);
Finally, to answer your question regarding, “Finding a solution to the system of equations f(x,y) - 10 = 0 and g(x,y,z) = 0 in the form y=k(x) yields five solution equations because it is a fifth-order equation. Therefore, differentiation must also be performed on the five equations, which is very tedious. Therefore, I am thinking that if we can obtain the solution as an implicit function, it will become one equation. I would like to know how to calculate it.”
You can use fsolve function, which can find the roots of a system of nonlinear equations. As an example, define your system of equations f(x,y) - 10 = 0 and g(x,y,z) = 0 in MATLAB and represent these equations as anonymous functions. Next, define the implicit function that combines your system of equations. You can create a function that returns a vector of the equations. Now, use the fsolve function to find the roots of the implicit function. Make sure to provide an initial guess for the variables and call fsolve with your implicit function, something like this
% Initial guess for variables x, y, z
initialGuess = [0, 0, 0];
% Solve the implicit function using fsolve
solution = fsolve(@implicitFunc, initialGuess);
To: Umar - san
Thank you for your detailed explanation.
Unfortunately, what I want is not dz/dx and dz/dy for z=g(x,y), but dz/du and dz/dv for the new curve z=h(u,v) where z=g(x,y) and f(x,y)=0 cross.
More troublesome is that z=g(x,y) and f(x,y)=0 are fifth order expressions, so there are five equations, which complicates the process.
If possible, could you please give me a concrete example of your next instruction?
"Make sure to provide an initial guess for the variables and call fsolve with your implicit function, something like this"
Torsten
Torsten on 1 Aug 2024
Edited: Torsten on 2 Aug 2024
Unfortunately, what I want is not dz/dx and dz/dy for z=g(x,y), but dz/du and dz/dv for the new curve z=h(u,v) where z=g(x,y) and f(x,y)=0 cross.
z = h(u,v) is always a surface, not a curve. The parametrization of a curve depends on one parameter, not two.
This might be of interest:
To: Torsten - san
Thanks for pointing this out.
With v as a variable, the correct equation is: z=h(k(v),v) I would like to calculate this equation.
Is there a better way to do this?

Hi @高木 範明 ,

Sorry, it took some time to research by going through some online publications. You mentioned, “what I want is not dz/dx and dz/dy for z=g(x,y), but dz/du and dz/dv for the new curve z=h(u,v) where z=g(x,y) and f(x,y)=0 cross. More troublesome is that z=g(x,y) and f(x,y)=0 are fifth order expressions, so there are five equations, which complicates the process.”

So, a simple way to solve will be first defining symbolic variables x, y, z, u, and v, g(x,y) and f(x,y) as fifth-order expressions. Then, set up the system of equations based on z=g(x,y) and f(x,y)=0 and solve the system of equations to find the intersection points. Then, calculate the partial derivatives dz/du and dz/dv and finally generate a 3D plot to visualize the curve z=h(u,v) based on the solutions.

 syms x y z u v; % Define symbolic variables

% Define g(x,y) and f(x,y) as fifth-order expressions

g = x^5 + y^5;

f = x^5 - y^5;

% Set up the system of equations

eq1 = z - g;

eq2 = f;

% Solve the system of equations

[solx, soly, solz] = solve([eq1 == 0, eq2 == 0], [x, y, z]);

% Calculate partial derivatives dz/du and dz/dv

dzdu = diff(solz, u);

dzdv = diff(solz, v);

% Generate 3D plot

[u_values, v_values] = meshgrid(-10:0.1:10, -10:0.1:10);

z_values = subs(solz, {x, y}, {u_values, v_values});

> % Define the u, v, and z values for the surface plot

u_values = linspace(-2, 2, 100);

v_values = linspace(-2, 2, 100);

[u_mesh, v_mesh] = meshgrid(u_values, v_values);

z_values = sin(u_mesh) .* cos(v_mesh);

% Create the 3D surface plot

figure;

surf(u_mesh, v_mesh, z_values);

xlabel('u');

ylabel('v');

zlabel('z');

title('3D Plot of z = h(u,v)');

Please see attached plot.

Then, I modified the above code to add the hold on; command to enable plotting intersect points on the existing 3D surface plot by using scatter3 function to plot the intersect points calculated earlier in red color. This modification enhances the visualization by showing both the surface plot and the intersect points in the same 3D plot.

% Create the 3D surface plot with intersect points

figure;

surf(u_mesh, v_mesh, z_values);

hold on; % Enable plotting intersect points

scatter3(solx, soly, solz, 'r', 'filled'); % Plot intersect points in red

xlabel('u');

ylabel('v');

zlabel('z');

title('3D Plot of z = h(u,v) with Intersect Points');

Now, addressing your query, “if possible, could you please give me a concrete example of your next instruction? Make sure to provide an initial guess for the variables and call fsolve with your implicit function, something like this"

To calculate dz/du and dz/dv for the new curve z=h(u,v) where z=g(x,y) and f(x,y)=0 intersect, you can use the implicit function theorem. Here's a simplified example to illustrate the process , assuming z=g(x,y) and f(x,y)=0 are given by:

% Define g(x,y) and f(x,y) functions

syms x y z

g = x^2 + y^2 - 1; % Example function g(x,y)

f = x^2 - y^2; % Example function f(x,y)

To find the intersection point, you can use fsolve with an initial guess:

% Define the system of equations

eq1 = @(vars) vars(1)^2 + vars(2)^2 - 1;

eq2 = @(vars) vars(2) - vars(3);

eq3 = @(vars) vars(1) + vars(3)^2 - 2;

% Updated initial guess for x, y, z

initial_guess = [0.1, 0.1, 0.1]; % Adjusted initial guess

% Updated options for fsolve with tighter function tolerance

options = optimoptions('fsolve', 'FunctionTolerance', 1e-10); % Tightened function tolerance

% Solve the system of equations with the updated options

solution = fsolve(@(vars) [eq1(vars), eq2(vars), eq3(vars)], initial_guess, options);

disp(solution); % Display the solution

Note: After executing system of equations, it displayed following message

“No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance. criteria details 0.4007 0.9815 1.2247

The reason solver stopped without finding a solution because the stopping criteria indicates that the gradient is regular, but the function values are not close to zero based on the function tolerance. So, this outcome is suggesting that the initial guess or the equations themselves may need adjustment to achieve convergence. So, as you can see when dealing with nonlinear systems of equations that fsolve struggles to converge on, you need to use an alternative method for a different solver or algorithm. So, one effective approach that I could think of was using the lsqnonlin function in MATLAB, which is specifically designed for solving nonlinear least-squares problems.

So, after executing system of equations again, it found the solution as described below.

% Define the system of equations

eq1 = @(vars) vars(1)^2 + vars(2)^2 - 1;

eq2 = @(vars) vars(2) - vars(3);

eq3 = @(vars) vars(1) + vars(3)^2 - 2;

% Updated initial guess for x, y, z

initial_guess = [0.1, 0.1, 0.1];

% Define the objective function for lsqnonlin

objective = @(vars) [eq1(vars); eq2(vars); eq3(vars)];

% Solve the system of equations with lsqnonlin

options = optimoptions('lsqnonlin', 'FunctionTolerance', 1e-15,

'MaxFunctionEvaluations', 1e4);

solution = lsqnonlin(objective, initial_guess, [], [], options);

disp(solution); % Display the solution

Local minimum found.Optimization completed because the size of the gradient is less thanthe value of the optimality tolerance. criteria details 0.4007 0.9815 1.2247

So, you can see that the message "Local minimum found" indicates that the optimization algorithm has converged to a local minimum, which means it has found a solution where the objective function cannot be further minimized in the vicinity of the current solution. The additional message about the gradient size being less than the optimality tolerance signifies that the algorithm stopped because the gradient (a measure of the function's slope) at the solution point was sufficiently small, indicating proximity to a minimum. The values displayed after the stopping criteria details represent the solution found by lsqnonlin for the variables in the system of equations.

Finally, create a 3D plot showing the intersection point:

% Plot the intersection point

figure;

plot3(solution(1), solution(2), solution(3), 'ro', 'MarkerSize', 10);

xlabel('x');

ylabel('y');

zlabel('z');

title('Intersection Point of z=g(x,y) and f(x,y)=0');

grid on;

I will also suggest utilization of an efficient numerical method like Newton's method to showcase the results besides the lsqnonlin function.

Let z = g(x, y) be the original function., f(x, y) = 0 represent the constraint equation. Define the new curve as z = h(u, v). by having your system involving five equations due to the fifth-order expressions. So, consider a specific example to illustrate the process. Suppose we have the following functions:

g(x, y) = x^2 + y^2 - 25

f(x, y) = x + y - 7

The aim is to find the partial derivatives dz/du and dz/dv for z = h(u, v). So, for the numerical method, you need an initial guess for u and v. I will assume, Initial guess: u = 1, v = 2.

Now, using Newton's method to iteratively solve the system of equations by using algorithm involving updating the initial guess until convergence is achieved.

% Define the functions g(x, y) and f(x, y)

g = @(x, y) x^2 + y^2 - 25;

f = @(x, y) x + y - 7;

% Define the partial derivatives of g and f

dh_du = @(x, y) 2*x;

dh_dv = @(x, y) 2*y;

df_dx = @(x, y) 1;

df_dy = @(x, y) 1;

% Initial guess for u and v

u = 1;

v = 2;

% Newton's method iteration

tolerance = 1e-6;

max_iterations = 100;

iteration = 0;

while iteration < max_iterations

    % Compute the Jacobian matrix
    J = [dh_du(u, v), dh_dv(u, v); df_dx(u, v), df_dy(u, v)];
    % Compute the function values
    F = [g(u, v); f(u, v)];
    % Solve for the increment
    delta = -J \ F;
    % Update the variables
    u = u + delta(1);
    v = v + delta(2);
    % Check for convergence
    if norm(delta) < tolerance
        break;
    end
    iteration = iteration + 1;

end

% Display the final values of u and v

disp(['Final values: u = ', num2str(u), ', v = ', num2str(v)]);

So, in this example, I have showcased how to calculate partial derivatives for a new curve defined by z = h(u, v) using Newton's method by defining the variables, providing an initial guess, and implementing the numerical method, which can efficiently compute the desired results.

Umar
Umar on 2 Aug 2024
Edited: Umar on 2 Aug 2024
@ 高木 範明,
With v as a variable, the correct equation is: z=h(k(v),v) I would like to calculate this equation.Is there a better way to do this?
Since you are interested in parametrizing a curve, that is the way equation should be structured z = h(k(v),v) which will ensure a proper representation of the curve and it also allows for a more accurate and efficient way to solve the parametrization of the curve within the Matlab environment.
With v as a variable, the correct equation is: z=h(k(v),v) I would like to calculate this equation.
If it were possible to solve your 2 equations in 3 unknowns, the usual parametric form for the implicitly defined curve in 3d is
c(t) = (c1(t),c2(t),c3(t))
for t taken from an 1d-interval.

Hi @ 高木 範明,

To address, “ the usual parametric form for the implicitly defined curve in 3d is c(t) = (c1(t),c2(t),c3(t)) for t taken from an 1d-interval.”

Again, you have to define the functions h and k based on the given equation z=h(k(v),v).Create a parametric form for the curve c(t) = (c1(t), c2(t), c3(t)) where t is within a 1D interval and then solve the system of equations to find the values of c1(t), c2(t), and c3(t) for the given interval of t. Please see attached plot.

=======================
To: Umar - san
% => We have included our comments between the lines.
% => Thank you for your detailed explanation. Although I did not understand some parts, I will close this case because I can do what I want to do.
=======================
Umar 2024 8 2 3:50
Hi @高木 範明 ,
Sorry, it took some time to research by going through some online publications. You mentioned, what I want is not dz/dx and dz/dy for z=g(x,y), but dz/du and dz/dv for the new curve z=h(u,v) where z=g(x,y) and f(x,y)=0 cross. More troublesome is that z=g(x,y) and f(x,y)=0 are fifth order expressions, so there are five equations, which complicates the process.
% => I apologize for the great inconvenience.
So, a simple way to solve will be first defining symbolic variables x, y, z, u, and v, g(x,y) and f(x,y) as fifth-order expressions. Then, set up the system of equations based on z=g(x,y) and f(x,y)=0 and solve the system of equations to find the intersection points. Then, calculate the partial derivatives dz/du and dz/dv and finally generate a 3D plot to visualize the curve z=h(u,v) based on the solutions.
% => Yes, I understand the story.
syms x y z u v; % Define symbolic variables
% Define g(x,y) and f(x,y) as fifth-order expressions
g = x^5 + y^5;
f = x^5 - y^5;
% Set up the system of equations
eq1 = z - g;
eq2 = f;
% Solve the system of equations
[solx, soly, solz] = solve([eq1 == 0, eq2 == 0], [x, y, z]);
% => solx=0, soly=0, solz=0
% Isn't z a value, not a symbol?
% Calculate partial derivatives dz/du and dz/dv
dzdu = diff(solz, u);
dzdv = diff(solz, v);
% => Since solx=0, soly=0, solz=0,
% we have dz/du=0 dz/dv=0. (Cannot get the equation.)
% Generate 3D plot
[u_values, v_values] = meshgrid(-10:0.1:10, -10:0.1:10);
z_values = subs(solz, {x, y}, {u_values, v_values});
% => Define a mesh grid of (u,v).
% Define the u, v, and z values for the surface plot
u_values = linspace(-2, 2, 100);
v_values = linspace(-2, 2, 100);
[u_mesh, v_mesh] = meshgrid(u_values, v_values);
% => Here again, different (u,v) mesh grids are defined. ??
z_values = sin(u_mesh) .* cos(v_mesh);
% Create the 3D surface plot
figure;
surf(u_mesh, v_mesh, z_values);
xlabel('u');
ylabel('v');
zlabel('z');
title('3D Plot of z = h(u,v)');
% Please see attached plot.
% Then, I modified the above code to add the hold on; command to enable plotting intersect points on the existing 3D surface plot by using scatter3 function to plot the intersect points calculated earlier in red color. This modification enhances the visualization by showing both the surface plot and the intersect points in the same 3D plot.
% Create the 3D surface plot with intersect points
figure;
surf(u_mesh, v_mesh, z_values);
hold on; % Enable plotting intersect points
scatter3(solx, soly, solz, 'r', 'filled'); % Plot intersect points in red
% => Since solx, soly, and solz are all zero, a red dot appears at (0,0,0).
xlabel('u');
ylabel('v');
zlabel('z');
title('3D Plot of z = h(u,v) with Intersect Points');
% Now, addressing your query, if possible, could you please give me a concrete example of your next instruction? Make sure to provide an initial guess for the variables and call fsolve with your implicit function, something like this"
To calculate dz/du and dz/dv for the new curve z=h(u,v) where z=g(x,y) and f(x,y)=0 intersect, you can use the implicit function theorem. Here's a simplified example to illustrate the process , assuming z=g(x,y) and f(x,y)=0 are given by:
% Define g(x,y) and f(x,y) functions
syms x y z
g = x^2 + y^2 - 1; % Example function g(x,y)
f = x^2 - y^2; % Example function f(x,y)
% To find the intersection point, you can use fsolve with an initial guess:
% Define the system of equations
eq1 = @(vars) vars(1)^2 + vars(2)^2 - 1;
eq2 = @(vars) vars(2) - vars(3);
eq3 = @(vars) vars(1) + vars(3)^2 - 2;
% Updated initial guess for x, y, z
initial_guess = [0.1, 0.1, 0.1]; % Adjusted initial guess
% Updated options for fsolve with tighter function tolerance
options = optimoptions('fsolve', 'FunctionTolerance', 1e-10); % Tightened function tolerance
% Solve the system of equations with the updated options
solution = fsolve(@(vars) [eq1(vars), eq2(vars), eq3(vars)], initial_guess, options);
disp(solution); % Display the solution
% => I am not sure about this, including the convergence method,
% so let me learn a bit about it.
% Note: After executing system of equations, it displayed following message
% No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance. criteria details 0.4007 0.9815 1.2247
% The reason solver stopped without finding a solution because the stopping criteria indicates that the gradient is regular, but the function values are not close to zero based on the function tolerance. So, this outcome is suggesting that the initial guess or the equations themselves may need adjustment to achieve convergence. So, as you can see when dealing with nonlinear systems of equations that fsolve struggles to converge on, you need to use an alternative method for a different solver or algorithm. So, one effective approach that I could think of was using the lsqnonlin function in MATLAB, which is specifically designed for solving nonlinear least-squares problems.
% So, after executing system of equations again, it found the solution as described below.
% Define the system of equations
eq1 = @(vars) vars(1)^2 + vars(2)^2 - 1;
eq2 = @(vars) vars(2) - vars(3);
eq3 = @(vars) vars(1) + vars(3)^2 - 2;
% Updated initial guess for x, y, z
initial_guess = [0.1, 0.1, 0.1];
% Define the objective function for lsqnonlin
objective = @(vars) [eq1(vars); eq2(vars); eq3(vars)];
% Solve the system of equations with lsqnonlin
options = optimoptions('lsqnonlin', 'FunctionTolerance', 1e-15,'MaxFunctionEvaluations', 1e4);
solution = lsqnonlin(objective, initial_guess, [], [], options);
disp(solution); % Display the solution
% => I understood that this one has good convergence.
% Local minimum found.Optimization completed because the size of the gradient is less thanthe value of the optimality tolerance. criteria details 0.4007 0.9815 1.2247
% So, you can see that the message "Local minimum found" indicates that the optimization algorithm has converged to a local minimum, which means it has found a solution where the objective function cannot be further minimized in the vicinity of the current solution. The additional message about the gradient size being less than the optimality tolerance signifies that the algorithm stopped because the gradient (a measure of the function's slope) at the solution point was sufficiently small, indicating proximity to a minimum. The values displayed after the stopping criteria details represent the solution found by lsqnonlin for the variables in the system of equations.
%Finally, create a 3D plot showing the intersection point:
% Plot the intersection point
figure;
plot3(solution(1), solution(2), solution(3), 'ro', 'MarkerSize', 10);
xlabel('x');
ylabel('y');
zlabel('z');
title('Intersection Point of z=g(x,y) and f(x,y)=0');
grid on;
% => Aside from the details, I understood the story.
% I will also suggest utilization of an efficient numerical method like Newton's method to showcase the results besides the lsqnonlin function.
% Let z = g(x, y) be the original function., f(x, y) = 0 represent the constraint equation. Define the new curve as z = h(u, v). by having your system involving five equations due to the fifth-order expressions. So, consider a specific example to illustrate the process. Suppose we have the following functions:
g(x, y) = x^2 + y^2 - 25
f(x, y) = x + y – 7
% The aim is to find the partial derivatives dz/du and dz/dv for z = h(u, v). So, for the numerical method, you need an initial guess for u and v. I will assume, Initial guess: u = 1, v = 2.
% Now, using Newton's method to iteratively solve the system of equations by using algorithm involving updating the initial guess until convergence is achieved.
% Define the functions g(x, y) and f(x, y)
g = @(x, y) x^2 + y^2 - 25;
f = @(x, y) x + y - 7;
% Define the partial derivatives of g and f
dh_du = @(x, y) 2*x;
dh_dv = @(x, y) 2*y;
df_dx = @(x, y) 1;
df_dy = @(x, y) 1;
% Initial guess for u and v
u = 1;
v = 2;
% Newton's method iteration
tolerance = 1e-6;
max_iterations = 100;
iteration = 0;
while iteration < max_iterations
% Compute the Jacobian matrix
J = [dh_du(u, v), dh_dv(u, v); df_dx(u, v), df_dy(u, v)];
% Compute the function values
F = [g(u, v); f(u, v)];
% Solve for the increment
delta = -J \ F;
% Update the variables
u = u + delta(1);
v = v + delta(2);
% Check for convergence
if norm(delta) < tolerance
break;
end
iteration = iteration + 1;
end
% Display the final values of u and v
disp(['Final values: u = ', num2str(u), ', v = ', num2str(v)]);
% So, in this example, I have showcased how to calculate partial derivatives for a new curve defined by z = h(u, v) using Newton's method by defining the variables, providing an initial guess, and implementing the numerical method, which can efficiently compute the desired results.

Sign in to comment.

 Accepted Answer

Hi @ 高木 範明,

Sorry, forgot to address your query regarding, “the correct equation is: z=h(k(v),v) I would like to calculate this equation”

To calculate the equation z = h(k(v), v), you need to understand how to handle functions of one variable within a surface function. In this case, k(v) is a function of v, and z = h(k(v), v) represents a surface where z depends on both k(v) and v. I will first define the functions h(u, v) and k(v).

% Define the function h(u, v)

function z = h(u, v)

    z = u^2 + v^2; % Example function, replace with your actual function

end

% Define the function k(v)

function result = k(v)

    result = 2*v; % Example function, replace with your actual function

end

Next, calculate z = h(k(v), v) using the defined functions by calling the functions k(v) and h(u, v) with appropriate values of v.

v = 3; % Set a value for v

u = k(v); % Calculate k(v)

z = h(u, v); % Calculate z = h(k(v), v)

disp(['The value of z for v = ', num2str(v), ' is ', num2str(z)]);

As an option, if you want to visualize the surface z = h(k(v), v), you can create a meshgrid and plot the surface using the calculated values of z.

% Create a meshgrid for v and u

[v, u] = meshgrid(-10:0.1:10, -10:0.1:10);

% Calculate z = h(k(v), v) for the meshgrid

z = h(k(v), v);

% Plot the surface

surf(u, v, z);

xlabel('u');

ylabel('v');

zlabel('z');

title('Surface Plot of z = h(k(v), v)');

2 Comments

To: Umar - san
Thank you very much for your kind explanation. I will read your two previous comments carefully.
Regarding the one previous explanation, I will write my understanding.
If z=(u,v)=u^2+v^2, then (u,v) is a point on a plane and z is a 3D-surface.
On the other hand, if the constraints z=(k(v),v) and k(v)=2v exist, then (u,v)=(k(v),v) becomes the line (2v,v). Therefore, z=(2v)^2+v^2, which is not 3D-survace but 3D-curve.
If my understanding is wrong, I would appreciate your clarification.
Hi @ 高木 範明,
Your interpretation is accurate in distinguishing between 3D surfaces and curves based on the given expressions.
z=(u,v)=u^2+v^2, represents a 3D surface in space and z=(2v)^2+v^2 actually represents a 3D curve, specifically a line in space.
I will look forward to your response regarding,“I will read your two previous comments carefully.Regarding the one previous explanation, I will write my understanding.”

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2023b

Community Treasure Hunt

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

Start Hunting!