z = 
find a zero of a two-variable function
이전 댓글 표시
my main.m is:
clear
clc
close all
%
X0 = [9.609 , 32.288]; %initial value close to zero of function
%
f = Trajectory(X0);
The script Trajectory.m is a messy think that returns a value of f. I want to find values of tht two input variables (X0) is the initial vbalues that give f=0
댓글 수: 8
Dyuman Joshi
2024년 4월 20일
Bruno Luong
2024년 4월 20일
편집: Bruno Luong
2024년 4월 20일
FSOLVE should also work for 0 , no? I believe it calls FMINCON to minimize norm(f(X))^2 in turn.
It doesn't have unique solition for sure in this case
Feel free to correct me if my visualization is inaccurate. In general, a typical bivariate function
will produce a surface. If you aim to solve the equation f(x, y) = 0, the real solution will yield a line segment, unless the surface is a Light Cone. For instance, solving
will lead to the real solution
, a straight line.
will lead to the real solution What outcome do you anticipate?
[X, Y] = meshgrid(-1:0.05:1);
Z = X.^3 + Y.^3;
surfc(X, Y, Z, 'FaceAlpha', 0.5)
shading interp
xlabel x, ylabel y, zlabel f(x,y), title('Two-variable function, f(x, y)')
Douw Gerbrand
2024년 4월 20일
Initially, I assumed that you had a predetermined path along a fixed surface and were seeking to determine a set of 2D coordinates on that static surface geometry, as demonstrated by @John D'Errico. However, based on your clarification, it seems that you are dealing with a motion system, indicated by the keywords "Trajectory" and "ODE."
Consequently, I am now attempting to visualize the three-dimensional shape of your positive definite cost function. The two parameters in the vector X are not coordinates per se, but rather factors that influence the trajectory motion. These factors can determine whether the trajectory is longer or shorter, and faster or slower in reaching a given endpoint. Such variations will directly impact the "shape" of the positive definite cost function.

Sam Chak
2024년 4월 21일
Is 'Trajectory' a script or a function? If it is a function, what is the output of the function?
function output = Trajectory()
output = ...;
end
Douw Gerbrand
2024년 4월 22일
답변 (3개)
If you are not unlucky
Trajectory = @(x) sum(x.^2,2)-34^2-1; % Test function, use use rather own
X0 = [9.609 , 32.288]; %initial value close to zero of function
f1 = @(dx1) Trajectory(X0+[dx1 0]);
dx1 = fzero(f1,0);
X = X0+[dx1 0];
disp(X)
Trajectory(X)
fimplicit(@(x,y) Trajectory([x(:) y(:)]).')
hold on
plot(X(1),X(2),'or')
댓글 수: 2
Douw Gerbrand
2024년 4월 20일
Bruno Luong
2024년 4월 20일
편집: Bruno Luong
2024년 4월 20일
As I said I gave a simple Trajectory as example, you don't have to use it but plug in your complicated code.on this
X0 = [9.609 , 32.288]; %initial value close to zero of function
f1 = @(dx1) Trajectory(X0+[dx1 0]);
dx1 = fzero(f1,0);
X = X0+[dx1 0];
Trajectory(X)
disp(X)
A two variable function is a surface. (@Sam Chak said this, but did not take the idea to the point of completion.)
But if a surface has any solution, where it becomes zero, then almost always it will have infinitely many solutions. In fact, the tool which draws the zeros of such a function is usually called contour. That is, you want to effectively find a contour plot.
Yes, a contour plot usually is drawn with many contours at different levels, but a contour plot is what you want, all the same. You want to see only ONE contour drawn, at z == 0. Again, a contour plot can be thought of as the set of values (x,y), suze that z(x,y) is a level surface, so z(x,y) is equal to some given constant, here zero.
For an example, I'll use a symbolic function, as there are nice tools we can use. But a function handle will also work, or just an m-file. ANY function of two independent variables will work. Of course, you want it to be a continuous function, as if not, things will get very messy.
syms x y
z = x^3 - y^3 + x^2*y^2 - 3
again, we cannot simply solve for a zero of that function, as there will be infintiely many such zeros, if any exist. We might do this:
H = fcontour(z);
H.LevelList = 0;
grid on
The curve drawn in cyan is the locus of points that solve the problem z(x,y)==0, so the level set of z, at z==0. As I said, not one solution. The problem is, a tool like fsolve will produce only one solution, it will not understand there are infinitely many such solutions, just finding only one point on one of those curves, and the solution it does find will be a function of the starting value you give it.
And of course, finding the "equation" of those curves here will be a very complicated looking thing, and that is for a rather simple problem to write. We can actually find at least a set of points that will approximate those curves, using the contourc function.
So what is it you want to see? Just a plot of the solutions? Then use tools like fcontour, or contour. I could also have used fimplicit. If your goal is a set of solutions, then we could use contourc. For example...
Xv = linspace(-5,5);
Yv = linspace(-5,5);
[X,Y] = meshgrid(Xv,Yv);
% Note my careful use of the dotted operators here to vectorize the code
% and make it properly evaluated for arrays X and Y.
zfun = @(x,y) x.^3 - y.^3 + x.^2.*y.^2 - 3;
Z = zfun(X,Y);
xy = contourc(Xv,Yv,Z,[0 0])
These are the solutions, split into two curves. Unfortunately contourc puts them into one array. Looking at the first column, we see [0;64]. That tells us the first contour had z==0, and there were 64 points found on that curve. The second branch of solutions had 146 points on it that were found.
So, if you want a set of solutions, then the simple approach is to use contourc. If you want only a plot, then just use one of the contour plotting tools.
댓글 수: 6
Douw Gerbrand
2024년 4월 20일
Douw Gerbrand
2024년 4월 20일
John D'Errico
2024년 4월 21일
Yes. then you are NOT looking to find a zero of the function, but its MINIMUM value. That is simply achieved using one of many tools, perhaps fminsearch, perhaps fminunc, or fmincon, or GA, etc.
Douw Gerbrand
2024년 4월 21일
Douw Gerbrand
2024년 4월 21일
Douw Gerbrand
2024년 4월 21일
Bruno Luong
2024년 4월 20일
이동: Bruno Luong
2024년 4월 22일
0 개 추천
@Douw Gerbrand If your objective function f is pd quadratic form of 2 variable X, then I believe you should minimize f rather than solve for f = 0.
Also if you use MATLAB ode solver, be careful that it can be an issue of optimizing parameters, see an example here : https://www.mathworks.com/matlabcentral/answers/2105041-using-fmincon-to-solve-objective-function-in-integral-form?s_tid=srchtitle
If you have only 2 parameters to estimate you could use fminsearch.
댓글 수: 2
Bruno Luong
2024년 4월 21일
이동: Bruno Luong
2024년 4월 22일
Try
X0 = [9.609 , 32.288]; %initial value close to zero of function
X = fminsearch(@(X)Trajectory(X),X0);
% or alternatively a simpler
X = fminsearch(@Trajectory,X0);
Or if it throw an error, please explain us at least the declaration signature of Trajectory and what does it suppose to do precisely with the input/output arguments.
Douw Gerbrand
2024년 4월 22일
이동: Bruno Luong
2024년 4월 22일
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


