Solving trigonometric non-linear equations in MATLAB
Show older comments
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 Comments
Walter Roberson
on 3 Sep 2016
There are (at least) two x solutions per cycle of 2 * Pi, and (at least) two y solutions per cycle of 2 * Pi. If you have the symbolic toolbox then you should be able to get exact solutions (to within the accuracy that "740.78 represents)
AVoyage
on 3 Sep 2016
John D'Errico
on 3 Sep 2016
Walter told you exactly how to solve it, and to do so trivially.
AVoyage
on 4 Sep 2016
Accepted Answer
More Answers (2)
Sumera Yamin
on 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 Comments
Walter Roberson
on 1 Jun 2018
You have three equations in two unknowns. You can solve the first two equations together; when you then substitute the values into the third equation, you will get 0.07334537675 instead of 0, indicating that the three equations are inconsistent with each other.
Sumera Yamin
on 4 Jun 2018
It means if i only used two equations instead of 3, then the solution will be possible?
Torsten
on 4 Jun 2018
A solution for the two equations would be possible, but not for all three.
sanjay kolape
on 9 Aug 2020
Edited: Walter Roberson
on 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);
1 Comment
Walter Roberson
on 9 Aug 2020
It is not clear how this code answers the original Question ?
Categories
Find more on Creating and Concatenating Matrices 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!