Unable to solve linear equation
22 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm new to Matlab and I've been trying multiple different things to solve a linear system equation.
I trying this right now
clear
a = 1.05
r = 0.60
m = 500
mc = 900
w = (m / 1000) * 9.81
wc = (mc / 1000) * 9.81
syms t2 alpha teta h
eqn1 = (wc * cos(180 - alpha) + t2 * cos(teta)) == 0
eqn2 = w * -1 + wc * sin(180 - alpha) + t2 * sin(teta) == 0
eqn3 = (a / sin(180 - alpha - teta)) - (r / sin(alpha)) == 0
eqn4 = (h / sin(teta)) - (r / sin(90)) == 0
[A, B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [t2, alpha, teta, h])
x = linsolve(A, B)
But I'm getting this error
Error using mupadengine/feval_internal
Unable to convert to matrix form because the system does not seem to be linear.
Error in sym/equationsToMatrix (line 61)
T = eng.feval_internal('symobj::equationsToMatrix',eqns,vars);
My goal ultimetly is to get the values of h, t2, alpha and teta.
I'm kind of confused because my calculator is able to solve this. I dont really get the problem. I'm trying to "automate" the calculations since I will be running this with a bunch of dataset eventually.
I tried using vpasolve() but it gives me response far from the response my calculator gives me (wich makes way more sense in the context)
Thanks for your help.
2 个评论
Cris LaPierre
2023-7-8
A good place to start, then, is with how you solve this on your calculator. What do you enter into your calculator for the matrix A and vector b?
James Tursa
2023-7-8
You have sin( ) and cos( ) in your equations, so it doesn't look linear to me either. Also, it looks like you intend the arguments to be degrees since you have 90 and 180 in them. So maybe sind( ) and cosd( ) are more appropriate.
回答(3 个)
Shivam
2023-7-8
Hello Tristan, "equationsToMatrix" function in matlab used to convert linear equations into matrix form, for eg:-
syms x y z
eqns = [x+y-2*z == 0,
x+y+z == 1,
2*y-z == -5];
[A,b] = equationsToMatrix(eqns)
But I see your equations contain trignometric functions which makes it non-linear system .
You can refer this to solve systems of non-linear equations - fsolve
This is the implemention -
clear
a = 1.05;
r = 0.60;
m = 500;
mc = 900;
w = (m / 1000) * 9.81;
wc = (mc / 1000) * 9.81;
% Define anonymous functions for the equations
eqns = @(x) [
wc * cosd(180 - x(2)) + x(1) * cosd(x(3));
-w + wc * sind(180 - x(2)) + x(1) * sind(x(3));
(a / sind(180 - x(2) - x(3))) - (r / sind(x(2)));
(x(4) / sind(x(3))) - (r / sind(90))
];
% Set a better initial guess for the variables
x0 = [0.1; 30; 30; 0.1];
% Solve the system of equations numerically
options = optimoptions('fsolve', 'Display', 'iter'); % Optional: Display iterative process
x = fsolve(eqns, x0, options);
% Extract the individual variables from the solution
t2 = x(1);
alpha = x(2);
teta = x(3);
h = x(4);
% Display the results
fprintf('t2 = %.4f\n', t2);
fprintf('alpha = %.4f\n', alpha);
fprintf('teta = %.4f\n', teta);
fprintf('h = %.4f\n', h);
0 个评论
Walter Roberson
2023-7-8
编辑:Walter Roberson
2023-7-8
a = 1.05;
r = 0.60;
m = 500;
mc = 900;
w = (m / 1000) * 9.81;
wc = (mc / 1000) * 9.81;
syms t2 alpha teta h
eqn1 = (wc * cosd(180 - alpha) + t2 * cosd(teta)) == 0
eqn2 = w * -1 + wc * sind(180 - alpha) + t2 * sind(teta) == 0
eqn3 = (a / sind(180 - alpha - teta)) - (r / sind(alpha)) == 0
eqn4 = (h / sind(teta)) - (r / sind(90)) == 0
sol124 = solve([eqn1, eqn2, eqn4])
eqn3b = subs(lhs(eqn3)-rhs(eqn3), sol124)
fplot(eqn3b(1,:), [-100 100])
fplot(eqn3b(2,:), [-100 100])
vpasolve(eqn3b(1,:), 50)
vpasolve(eqn3b(2,:), -50)
0 个评论
Diwakar Diwakar
2023-7-8
syms t2 alpha teta h
a = 1.05;
r = 0.60;
m = 500;
mc = 900;
w = (m / 1000) * 9.81;
wc = (mc / 1000) * 9.81;
eqn1 = wc * cosd(180 - alpha) + t2 * cosd(teta) == 0;
eqn2 = -w + wc * sind(180 - alpha) + t2 * sind(teta) == 0;
eqn3 = a / sind(180 - alpha - teta) - r / sind(alpha) == 0;
solutions = vpasolve([eqn1, eqn2, eqn3], [t2, alpha, teta]);
t2_sol = solutions.t2;
alpha_sol = solutions.alpha;
teta_sol = solutions.teta;
fprintf('t2 = %.4f\n', t2_sol);
fprintf('alpha = %.4f\n', alpha_sol);
fprintf('teta = %.4f\n', teta_sol);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Equation Solving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!