How do I create a matrix from a simple for loop?

  • Thread starter Thread starter HACRS4
  • Start date Start date
  • Tags Tags
    Loop Matrix
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
HACRS4
Messages
3
Reaction score
0

Homework Statement



I am trying to create a code, in Matlab, that will describe the geometry of a PARSEC airfoil. The x coordinates for the upper and lower surfaces of the airfoil shape simply run from 0 to 1 (for a unit chord length). The z-coordinates (that describe the shape) are given by:

z_upper = sum (a_n * x ^ ((n-1) / 2) )

where the summation occurs over n = 1 to n =6. The coefficient a_n is determined by solving a system of linear equations - I think I have done this correctly.

The lower surface z-coordinates are similarly defined by with a different set of coefficients.

The way I have approached the problem is to try and create a matrix with 6 columns (for n = 1:6) and as many rows are there are x-coordinates. A vector containing all of the z-coordinates is then found by summing the values across each of the 6 columns for each row.

However, Matlab is only returning a 7x6 matrix instead of a matrix with as many rows are there are x-coordinates. It is also returning an 'index out of bounds' when creating the vector of z-coordinates - I think this is just because the defined matrix is too small.

My apologies if the solution is a simple one, I am new to coding.

Homework Equations



for n = 1:6

z_upper = Ʃ a_n * x ^ ((n-1)/2)

The Attempt at a Solution



% Test Values

x(1) = 0.01;

x(2) = 8;

x(3) = 12;

x(4) = 0.43;

x(5) = 0.12;

x(6) = -0.8;

x(7) = 0.23;

x(8) = -0.020;

x(9) = 0.1;

x(10) = -0.004;

x(11) = 0.003;

x(12) = 0.0030;

% x-coordinate range

x_coord = 0:0.01:1;

% upper surface coefficients (x_u = a_n)

% Ax = B

A_U = [ 1 1 1 1 1 1 ; x(4)^(1/2) x(4)^(3/2) x(4)^(5/2) x(4)^(7/2) x(4)^(9/2) x(4)^(11/2); 1/2 3/2 5/2 7/2 9/2 11/2;... (1/2)*x(4)^(-1/2) (3/2)*x(4)^(1/2) (5/2)*x(4)^(3/2) (7/2)*x(4)^(5/2) (9/2)*x(4)^(7/2) (11/2)*x(4)^(9/2);... -(1/4)*x(4)^(-3/2) (3/4)*x(4)^(-1/2) (15/4)*x(4)^(1/2) (35/4)*x(4)^(3/2) (63/4)*x(4)^(5/2) (99/4)*x(4)^(7/2);... 1 0 0 0 0 0];

B_U = [x(10) + (1/2)*x(12); x(5); tan((2*x(2) - x(3))/2); 0; x(6); sqrt(x(1))];

x_u = A_U\B_U;

% z (upper surface) coordinates

% z_upper = sum ( (x_u(n) * x_coord(n-1/2) )

for j = 1:6

for i = 1:length(x_coord)

z_mat(i,j) = x_u(j) * x_coord(i) ^ ((j-1)/2);

z_upper(i) = sum(z_mat(:,(i)));

end
end
 
Physics news on Phys.org
Sounds like you have not been careful to make sure that the various vectors are the right length.
Check that you are not calling an index that does not exist.

You seem to be under-using MATLAB - your approach makes it difficult to troubleshoot.
There are better ways to achieve your aim in MATLAB by using matrix equations.
i.e. you don't need to explicitly enter each member of a vector separately.

Try troubleshooting with smaller vectors, and make assigned dimensions into a variable you declair at the start.
i.e instead of j=1:6, do j=1:R and you have previously written R=6.

Check the size of each of the vectors that are used to make the matrix ...