Solve a system of equations
5 views (last 30 days)
Show older comments
Lorenzo Stabile
on 27 Feb 2019
Commented: Walter Roberson
on 28 Feb 2019
I have to solve a system of equations in the form Ax=b+F, where A is a known 8x8 matrix. Also the term F is all known.
The problem is that I have unknowns both in the term x and b (4 in x and 4 in b, the other terms are given by boundary conditions).
How can I set the problem on matlab?
Thanks.
0 Comments
Accepted Answer
Torsten
on 28 Feb 2019
function main
x0=zeros(8,1);
sol=fminsearch(@(x)fun(x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8)),x0)
sol=fminsearch(@(x)fun(x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8)),sol)
sol=fminsearch(@(x)fun(x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8)),sol)
sol=fminsearch(@(x)fun(x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8)),sol)
res=fun(sol(1),sol(2),sol(3),sol(4),sol(5),sol(6),sol(7),sol(8))
end
function res = fun(a1,a2,a3,a4,R1,R2,R3,R4)
l1=50;
l2=50;
l3=50;
Fc1=30;
Fc2=50;
Fc3=100;
a=25;
b=25;
c=25;
d=25;
e=25;
f=25;
v1=0;
v2=0;
v3=0;
v4=0;
m1=0;
m2=0;
m3=0;
m4=0;
A=[12/l1^2, 6/l1^2, -12/l1^3, 6/l1^2, 0, 0, 0, 0;...
6/l1^2, 4/l1^2, -6/l1^2, 4/l1^2, 0, 0, 0, 0; ...
-12/l1^3, -6/l1^2, 12/l1^3+12/l2^3, -6/l1^2+6/l2^2, -12/l2^3, 6/l2^2, 0, 0; ...
6/l1^2, 2/l1 -6/l1^2+6/l2^2, 4/l1+4/l2, -6/l2^2, 4/l2, 0, 0; ...
0, 0, -12/l2^3, -6/l2^2, 12/l2^3+12/l3^3, -6/l2^2+6/l3^2, -12/l3^3, 6/l3^2; ...
0, 0, 6/l2^2, 2/l2, -6/l2^2+6/l3^2, 4/l2+4/l3, -6/l3^2, 4/l3; ...
0, 0, 0, 0, -12/l3^3, -6/l3^2, 12/l3^3, -6/l3^2; ...
0, 0, 0, 0, 6/l3^2, 2/l3, -6/l3^2, 4/l3];
x=[v1, a1, v2, a2, v3, a3, v4, a4]';
s=[R1, m1, R2, m2, R3, m3, R4, m4]';
F=[(l1+2*a^3-3*l1*a^2)*Fc1/l1^3, (a^3+l1^2*a-2*l1*a)*Fc1/l1^3, ...
(2*a^3-3*l1*a^2)*Fc1/l1^2+(l2+2*c^3-3*l2*c^2)*Fc2/l2^3, (a^3-l1*a^2)*Fc1/l1^2+(c^3+l2^2*c-2*l2*c)*Fc2/l2^2, ...
(2*c^3-3*l2*c^2)*Fc2/l2^2+(l3+2*e^3-3*l3*e)*Fc3/l3^3, (c^3-l2*c^2)*Fc2/l2^2+(e^3+l3*e-2*l3*e)*Fc3/l3^2, ...
(2*e^3-3*l3*e^2)*Fc3/l3^3, (e^3-l3^2)*Fc3/l3^2]';
res=sum((A*x-s-F).^2);
end
0 Comments
More Answers (1)
Walter Roberson
on 27 Feb 2019
A = sym('A',[8 8],'real');
x = sym('x',[8 1]);
b = sym('b',[8 1]);
F = sym('F',[8 1]);
sol = solve(A*x == b+F,[x(1:4);b(5:8)]); %choose appropriate variables to solve for
sol will be a struct with one field for each variable solved for.
The above is the general solution, at least under the assumption that F does not depend upon the other variables.
It is not a fast solution. If you were to put in your known numeric values then it would probably go more quickly.
2 Comments
Walter Roberson
on 28 Feb 2019
Code attached. Solution is a small number of seconds once the Symbolic Toolbox has been initialized.
See Also
Categories
Find more on Equation Solving 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!