Solving trigonometric non-linear equations in MATLAB
23 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
AVoyage
il 3 Set 2016
Commentato: Walter Roberson
il 9 Ago 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;
%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 Commenti
Risposta accettata
John D'Errico
il 3 Set 2016
Modificato: John D'Errico
il 3 Set 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 Commento
John D'Errico
il 4 Set 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.
Più risposte (2)
Sumera Yamin
il 1 Giu 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 Commenti
Sumera Yamin
il 4 Giu 2018
It means if i only used two equations instead of 3, then the solution will be possible?
sanjay kolape
il 9 Ago 2020
Modificato: Walter Roberson
il 9 Ago 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);
1 Commento
Vedere anche
Categorie
Scopri di più su Calculus in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!