필터 지우기
필터 지우기

need help with this my fsolve is not working

조회 수: 4 (최근 30일)
Manuel Arellano
Manuel Arellano 2021년 12월 6일
댓글: Manuel Arellano 2021년 12월 6일
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

채택된 답변

John D'Errico
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])
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
  댓글 수: 1
Manuel Arellano
Manuel Arellano 2021년 12월 6일
it was my handle i wrote it incorrectly, thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by