z =
find a zero of a two-variable function
17 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Sam Chak
il 21 Apr 2024
Is 'Trajectory' a script or a function? If it is a function, what is the output of the function?
function output = Trajectory()
output = ...;
end
Risposte (3)
Bruno Luong
il 20 Apr 2024
Modificato: Bruno Luong
il 20 Apr 2024
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 Commenti
Bruno Luong
il 20 Apr 2024
Modificato: Bruno Luong
il 20 Apr 2024
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)
John D'Errico
il 20 Apr 2024
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 Commenti
Bruno Luong
il 20 Apr 2024
Spostato: Bruno Luong
il 22 Apr 2024
@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 Commenti
Bruno Luong
il 21 Apr 2024
Spostato: Bruno Luong
il 22 Apr 2024
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.
Vedere anche
Categorie
Scopri di più su Ordinary Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!