Unable to solve linear equation

15 views (last 30 days)
Tristan
Tristan on 8 Jul 2023
Answered: Diwakar Diwakar on 8 Jul 2023
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
a = 1.0500
r = 0.60
r = 0.6000
m = 500
m = 500
mc = 900
mc = 900
w = (m / 1000) * 9.81
w = 4.9050
wc = (mc / 1000) * 9.81
wc = 8.8290
syms t2 alpha teta h
eqn1 = (wc * cos(180 - alpha) + t2 * cos(teta)) == 0
eqn1 = 
eqn2 = w * -1 + wc * sin(180 - alpha) + t2 * sin(teta) == 0
eqn2 = 
eqn3 = (a / sin(180 - alpha - teta)) - (r / sin(alpha)) == 0
eqn3 = 
eqn4 = (h / sin(teta)) - (r / sin(90)) == 0
eqn4 = 
[A, B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [t2, alpha, teta, h])
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);
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 Comments
Cris LaPierre
Cris LaPierre on 8 Jul 2023
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
James Tursa on 8 Jul 2023
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.

Sign in to comment.

Answers (3)

Shivam
Shivam on 8 Jul 2023
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);

Walter Roberson
Walter Roberson on 8 Jul 2023
Edited: Walter Roberson on 8 Jul 2023
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
eqn1 = 
eqn2 = w * -1 + wc * sind(180 - alpha) + t2 * sind(teta) == 0
eqn2 = 
eqn3 = (a / sind(180 - alpha - teta)) - (r / sind(alpha)) == 0
eqn3 = 
eqn4 = (h / sind(teta)) - (r / sind(90)) == 0
eqn4 = 
sol124 = solve([eqn1, eqn2, eqn4])
sol124 = struct with fields:
h: [2×1 sym] t2: [2×1 sym] teta: [2×1 sym]
eqn3b = subs(lhs(eqn3)-rhs(eqn3), sol124)
eqn3b = 
fplot(eqn3b(1,:), [-100 100])
fplot(eqn3b(2,:), [-100 100])
vpasolve(eqn3b(1,:), 50)
ans = 
17.888764512725665933141448970789
vpasolve(eqn3b(2,:), -50)
ans = 

Diwakar Diwakar
Diwakar Diwakar on 8 Jul 2023
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);
t2 = -8.6836
fprintf('alpha = %.4f\n', alpha_sol);
alpha = 162.1112
fprintf('teta = %.4f\n', teta_sol);
teta = -14.6281

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!