Clear Filters
Clear Filters

What is wrong with my code?

1 view (last 30 days)
Khoirunnisa' Irodatillah
Khoirunnisa' Irodatillah on 4 Mar 2019
Commented: Walter Roberson on 5 Mar 2019
function y = slider(x,x2)
x0 = [3;60];
L2 = 2;
L1 = 4;
x2= 50;
y = [L2*cosd(x2) - x(1)*cosd(x(2)) - L1;
L2*sind(x2) - x(1)*sind(x(2))];
end
>> x_full = fsolve(@slider,x0);
>> Rao4 = x_full(1)
Rao4 =
-3.116953357712931
>> theta4 = x_full(2)
theta4 =
-29.441463984624214
It is a equation position for slide crank. The answer for Rao4 should be positive and theta 4 should be around 50 degrees, I try it many times but I don't know what is wrong. I checked with my friend's code, it's the same but still, we have different result. Thank you for your help.

Answers (1)

Walter Roberson
Walter Roberson on 4 Mar 2019
Your equation has an infinite number of solutions because of the trig, and has two basic forms. You are getting the primary solution to one of the basic forms. If you were to use different x0 you might end up with a different solution.
However, none of the infinite solutions has x(2) near 50 degrees: one of the solutions has an x(2) near 150.56 degrees though. The x(2) values are basically 180 degrees apart from that -29.4 that you produced.
  2 Comments
Khoirunnisa' Irodatillah
Khoirunnisa' Irodatillah on 5 Mar 2019
Ah.. I see.. How to make the answer only form 0 to 2pi? Thank you for your answer.
Walter Roberson
Walter Roberson on 5 Mar 2019
Or use the symbolic toolbox. vpasolve() permits you to specify bounds for variables.

Sign in to comment.

Categories

Find more on Symbolic Math 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!