fsolve wont take the initial guess
Show older comments
Hello,
I'm having a problem solving a NDF using fsolve with an event. There are 2 variables y(1) and y(2) where we need to do an order reduction getting a total of 4 variables to the ODE problem. Objective of the code is to find when will both variables be equal to x_wall. The code is as such
y0 = [pi/8 1 0 0]; %inital guess
t_step = 0.1; %time step
t_interv = [0 10];
x_wall = -0.5; %final position
Y=fzero(@(x) solver(t_step, t_interv, x, x_wall), y0 );
function F= solver(t_step, t_interv,x, x_wall)
l1=1;l2=1;
[t,y] = MY_RK4_event(@My_DoublePendulum, t_step, t_interv, x, x_wall);
X1 = sin(y(1,end))*l1;
X2 = X1+sin(y(2,end))*l2;
F=[X1 X2 0 0]-[x_wall x_wall 0 0]; %final condition
end
I keep getting an error saying that i can only input 2 inputs to the solver, and when inputing 2 inputs the fsolve only send in y0(1) as variable x in the function solver.
Why?
5 Comments
John D'Errico
on 7 Dec 2020
Edited: John D'Errico
on 7 Dec 2020
If you intend to use fsolve, fsolve might work better, if you tried to use it, instead of fzero. But then, who am I to complain?
I would point out that fzero is a solver that ONLY applies to ONE variable problems. As such, it will likely fail for your problem. It would also suggest this may be the reason MATLAB complains, because you seem to be trying to force fzero to think it is fsolve. In turn, this causes an identity crisis for the solver. I have heard it is now going to a psycho-therapist to recover from the mathematical trauma.
Anyway, actually using fsolve may improve your chances of making fsolve work. Does this make sense?
Nethanel Benzaquen
on 7 Dec 2020
John D'Errico
on 7 Dec 2020
Edited: John D'Errico
on 7 Dec 2020
You CANNOT use fzero to solve a mutliple variable problem. Of course, we are not told the complete problem statement, so knowing what you were really assigned to do is difficult.
All that I know is you were apparently told to do something effectively non-sensical, at least given what you claim. That means either you or your instructor is confused, since I know what I'm doing so it aint me. :) It may be your instructor who is confused, but I'm betting there is something you have misunderstood in what you think you were told to do.
So, let me start over.
- You claim to want to solve a 4-variable problem, although it looks like you are trying to hold two of those variables fixed. Regardless, 2 is still greater than 1 the last time I checked.
- You claim you were told to use fzero.
- Fzero CANNOT be used to solve multi-variable problems. And 2 is indeed greater than 1.
2 > 1
Do you see the disconnect?
So, what variable(s) do you need the solver to optimize here?
If I had to guess, the question is to find the TIME when both variables are equal to x_wall? Of course, even that is a problem, because now you want to solve for a single variable time, when TWO things happen concurrently.
Stephan
on 7 Dec 2020
Maybe my english language knowledge is to poor, but did you notice that you use fzero:
Y=fzero(@(x) solver(t_step, t_interv, x, x_wall), y0 );
instead of fsolve? Is it that simple?
Nethanel Benzaquen
on 7 Dec 2020
Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!