need help with this my fsolve is not working

17 views (last 30 days)
function F = root2d(x)
r1 = 8;
r2 = 3.5;
r3 = 10;
r4 = 9;
o2 = 15;
F(1) = r3.*cosd(x(1))-r4.*cosd(x(2))+(-r1+r2.*cosd(o2));
F(2) = r3.*sind(x(1))-r4.*sind(x(2)) + r2.*sind(o2);
end
in the command window this pops up
>> fsolve(root2d, [1 1])
Not enough input arguments.
Error in root2d (line 9)
F(1) = r3.*cosd(x(1))-r4.*cosd(x(2))+(-r1+r2.*cosd(o2));
can anyo please help me to see what i need to do to get this to run properly

Accepted Answer

John D'Errico
John D'Errico on 6 Dec 2021
Edited: John D'Errico on 6 Dec 2021
You were close. And I think what you did wrong was not completely obvious. Your function is below.
First, BEFORE YOU EVER TRY to use an optimizer. test the objective function. Verify that it works.
root2d([1 1])
ans = 1×2
-3.6194 0.9233
So it works. YEAY! Next, check that for different inputs, you get different outputs.
root2d([2 -3])
ans = 1×2
-3.6130 1.7259
That is the first thing you should ever do. Next, why is there a problem?
You need to tell fzsolve that what you are passing in is a FUNCTION. Like this:
fsolve(@root2d, [1 1])
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
ans = 1×2
52.9807 81.0408
Why is this different from what you did? You tried this, and got an error:
fsolve(root2d, [1 1])
So what does MATLAB do when it sees that code? It looks at root2d inside that call, and it tries to execute the function. But you gave root2d no arguments. MATLAB does not know at that point that what you want to do is to tell fsolve to USE that function. The way you tell fsolve to use something AS a function, is to create a function handle. That is what I did, and why it works. So again, you were close. Just one character away.
Remember that root2d is the name of a function to you. But to MATLAB, it is something that when it sees that, it tries to EVAUATE that function at some point, just as we did initially. But if we do this:
F = @root2d;
whos F
Name Size Bytes Class Attributes F 1x1 32 function_handle
So you see, F is a function handle. It is something you can pass around, and into another function to use it.
function F = root2d(x)
r1 = 8;
r2 = 3.5;
r3 = 10;
r4 = 9;
o2 = 15;
F(1) = r3.*cosd(x(1))-r4.*cosd(x(2))+(-r1+r2.*cosd(o2));
F(2) = r3.*sind(x(1))-r4.*sind(x(2)) + r2.*sind(o2);
end

More Answers (0)

Categories

Find more on Problem-Based Optimization Setup 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!