need help with this my fsolve is not working
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
John D'Errico
2021-12-6
编辑:John D'Errico
2021-12-6
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])
So it works. YEAY! Next, check that for different inputs, you get different outputs.
root2d([2 -3])
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])
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
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geometry and Mesh 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!