Solving trigonometric non-linear equations in MATLAB

24 Ansichten (letzte 30 Tage)
AVoyage
AVoyage am 3 Sep. 2016
Kommentiert: Walter Roberson am 9 Aug. 2020
Hi there, I'm trying to solve some non-linear simultaneous equations with trigonometric functions. They are of the form:
297.5*cos(x) + 489*sin(y) - 740.78 = 0;
297.5*sin(x) - 489*cos(y) + 197 = 0;
I have tried to follow this format and so currently have this:
%Mapping b(1) = x, b(2) = y
f = @(b) [297.5*cos(b(1)) + 489.5*sin(b(2)) -740.78; 297.5*sin(b(1)) - 489*cos(b(2)) +197];
B0 = rand(2,1)*2*pi;
[B,fv,xf,ops] = fsolve(f, B0);
ps = ['theta'; 'beta'];
fprintf(1, '\n\tParameters:\n')
for k1 = 1:length(B)
fprintf(1, '\t\t%s = % .4f\n', ps(k1,:), B(k1))
end
However, I am not getting any results. MATLAB says that the "last step was ineffective". Does this mean that my system is unsolveable or have I made a mistake in my code?
Thank you in advance!
  4 Kommentare
John D'Errico
John D'Errico am 3 Sep. 2016
Walter told you exactly how to solve it, and to do so trivially.
AVoyage
AVoyage am 4 Sep. 2016
Yes, sorry, I realise that. I meant fiddle with my underlying model that led to these equations because I have other systems of 4 and 5 non-linear simultaneous equations to solve later - just wanted to make sure that I got everything to work for the basic case first. To that end though I did use Walter's solution as a check on my answer, so thanks again Walter.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 3 Sep. 2016
Bearbeitet: John D'Errico am 3 Sep. 2016
This is not impossible to solve. The symbolic toolbox does it trivially. You can convert the result to a numeric one as I show.
syms x y
E1 = 297.5*cos(x) + 489*sin(y) - 740.78 == 0;
E2 = 297.5*sin(x) - 489*cos(y) + 197 == 0;
[x,y] = solve(E1,E2,x,y)
x =
2*atan(6674880334960949^(1/2)/548570849 - 73259375/548570849)
-2*atan(6674880334960949^(1/2)/548570849 + 73259375/548570849)
y =
-2*atan(6674880334960949^(1/2)/581777974 - 452801775/581777974)
2*atan(6674880334960949^(1/2)/581777974 + 452801775/581777974)
vpa(x)
ans =
0.030770500758327955535681743283777
-0.55061054417726048251968900912215
vpa(y)
ans =
1.1356089436618227709005164864045
1.4861436665090379405781196310366
Or you can solve it numerically using a tool like fsolve. First, why did you have a problem?
First, I'll write f as you did:
f = @(b) [297.5*cos(b(1)) + 489.5*sin(b(2)) -740.78; 297.5*sin(b(1)) - 489*cos(b(2)) +197];
f([1 2])
ans =
605.84 -740.78
453.83 197
See that when I Passed f a vector of length 2, it created a MATRIX result.
What happened when you wrote the sub-expression:
297.5*cos(b(1)) + 489.5*sin(b(2)) -740.78
MATLAB parsed that as a vector of length 2. See the differrnce between the way Ill write it:
f = @(b) [297.5*cos(b(1)) + 489.5*sin(b(2)) - 740.78; 297.5*sin(b(1)) - 489*cos(b(2)) + 197];
f([1 2])
ans =
-134.94
650.83
The only difference was I put a space between the operator - and the number 740.78. Then I did the same for the constant term in the second part.
Using the second form for f, as I defined it, fsolve now works trivially too.
b = fsolve(f,[1 2])
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
b =
0.033827 1.1336
It finds only one solution of course, based on the starting values provided.
  1 Kommentar
John D'Errico
John D'Errico am 4 Sep. 2016
Thinking about this question, I suppose the answer is somewhat non-obvious. It hinges on the difference between the fragments
[2 -1]
ans =
2 -1
and
[2 - 1]
ans =
1
I put them both in square brackets, but they are different animals. In the first case, MATLAB parses -1 as the application of unary minus to 1, then sees these two numbers are separated by a space, the equivalent to a comma. So it creates a vector of length 2.
In the second case, MATLAB sees the - operator between two numbers, so it applies the binary - operator between them. The result is now a scalar.
As I said, a subtle appearing difference, but an important one.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Sumera Yamin
Sumera Yamin am 1 Jun. 2018
hi john, can you comment on why my code (similar as the original question is not giving me any solution? I will really appreciate any help.
Ld= 0.8194 %constant
calib = @(K,L) [cos(K*L).^2 + Ld*K*sin(K*L).*cos(K*L) - 2.2; cos(K*L).^2 - 0.299; Ld*cos(K*L).^2 + (sin(K*L).*cos(K*L))/K - 0.262];
fun = @(b) calib(b(1),b(2));
initial_val=[2.73, 0.6]; % initial value of K and L respectively
[x,fval,exitflag,output] = fsolve(fun,initial_val)
  3 Kommentare
Sumera Yamin
Sumera Yamin am 4 Jun. 2018
It means if i only used two equations instead of 3, then the solution will be possible?
Torsten
Torsten am 4 Jun. 2018
A solution for the two equations would be possible, but not for all three.

Melden Sie sich an, um zu kommentieren.


sanjay kolape
sanjay kolape am 9 Aug. 2020
Bearbeitet: Walter Roberson am 9 Aug. 2020
% write code
l1 = 10; % length of first arm
l2 = 10; % length of second arm
X_coordinate = input("Enter value of X coordinate : "); % theta1 values
Y_coordinate = input("Enter value of Y coordinate : "); % theta2 values
x0 = 0; y0 = 0; %coordinate of fixed link
syms theta1 theta2
eqn1 = l1*cos(theta1) + l2*cos(theta1-theta2) == X_coordinate;
eqn2 = l1*sin(theta1) + l2*sin(theta1-theta2) == Y_coordinate;
[theta1, theta2]= solve(eqn1,eqn2,theta1,theta2);
theta1 = theta1(2);
theta2 = theta2(2);
x1 = l1*cos(theta1);
y2=l1*sin(theta1);
X1 = [x0 x1]; Y1= [y0 y1];
X2 = [x1 X_coordinate]; Y2 = [y1 Y_coordinate];
comet(X1,Y1);
hold on
comet(X2,Y2);

Community Treasure Hunt

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

Start Hunting!

Translated by