# How would I solve the following system of equations with a for loop?

1 view (last 30 days)
Alexandr on 30 Nov 2023
Moved: Torsten on 30 Nov 2023
z1 = 0;
-3*z1 + 4*z2 - z3 = 0;
z.m-2 - 4*z_m-1 + 6*z.m - 4*z.m+1 + z.m+2 = (-mu * g - f_m) * (delta_x^4)/(E*I);
m = 3:M-2;
%M is a number that determines the size of the array
z.M-2 - 4*z.M-1 + 3*z.M = 0;
z.M = 0;
These are the five equations I need to solve. I tried using Ax = b method but I can't figure out how to get it to work with the third equation. Below is my code.
A = zeros(5,M); %Array A is every constant before each elongation:z from equations 1-5. Rows correspond to each equation, columns correspond to each z.
for m = 3:M-2
A(3,(m-2):(m+2)) = [1,-4,6,-4,1]; % modifying array for the values of z from equation 3.
end
A(1,1) = 1; % modifying array for the values of z from equation 1.
A(2,1:3) = [-3,4,-1]; % modifying array for the values of z from equation 2.
A(4,(M-2):M) = [1,-4,3]; % modifying array for the values of z from equation 4.
A(5,M) = 1; % modifying array for the values of z from equation 5.
b3 = (-mu * g - f((round((M + 1)/2)))) * ((delta_x^4)/(E * I)); % b is a column vector of the left hand side of equations 1-5
b = [0;0;b3;0;0];
z = A\b; % Mx1 column vector z is found by using right side division.

Torsten on 30 Nov 2023
Moved: Torsten on 30 Nov 2023
Your matrix is not 5xM, but MxM.
The definition of A must read
A = zeros(M); %Array A is every constant before each elongation:z from equations 1-5. Rows correspond to each equation, columns correspond to each z.
A(1,1) = 1; % modifying array for the values of z from equation 1.
A(2,1:3) = [-3,4,-1]; % modifying array for the values of z from equation 2.
for m = 3:M-2
A(m,(m-2):(m+2)) = [1,-4,6,-4,1]; % modifying array for the values of z from equation 3.
end
A(M-1,(M-2):M) = [1,-4,3]; % modifying array for the values of z from equation 4.
A(M,M) = 1; % modifying array for the values of z from equation 5.
I think you know how the Mx1 vector b must be constructed.

### Categories

Find more on Linear Algebra in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!