%**************************************************************************
%*     Problem 8.2                                                        *
%*     filename: problem08_2.m                                            *
%*     Programed by Ryoichi Kawai for Computational Physics Course        *
%*     Revised on 02/10/2019.                                             *
%**************************************************************************
clear all;

k1=2; k2=4; k3=4; k4=2;
L1=1; L2=2; L3=1; L4=2;
d=8;

% equilibrium condition
% k1*(x1 - L1) - k2*(x2 - x1 - L2) = 0
% k2*(x2 - x1 - L2) - k3*(x3-x2-L3) = 0
% k3*(x3 - x2 - L3) - k4*(d-x3-L4) = 0

A = [[k1+k2,-k2,0]; [-k2,k2+k3,-k3]; [0,-k3,k3+k4]];
B = [k1*L1-k2*L2;k2*L2-k3*L3;k3*L3+k4*(d-L4)];

% solve A*x=B
x=linsolve(A,B);

fprintf('\nx1=%6.3f,   x2=%6.3f,  x3=%6.3f\n',x)