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

  • Thread starter Thread starter HACRS4
  • Start date Start date
  • Tags Tags
    Loop Matrix
Click For Summary
SUMMARY

This discussion focuses on creating a matrix in MATLAB to model the geometry of a PARSEC airfoil using a for loop. The user attempts to calculate the z-coordinates for the upper surface of the airfoil based on a polynomial defined by coefficients derived from a system of linear equations. The primary issue encountered is MATLAB returning a 7x6 matrix instead of the expected dimensions, along with an 'index out of bounds' error when summing the z-coordinates. Solutions include ensuring proper vector dimensions and utilizing MATLAB's matrix capabilities more effectively.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with matrix operations in MATLAB
  • Knowledge of polynomial equations and their coefficients
  • Basic concepts of airfoil geometry and its mathematical representation
NEXT STEPS
  • Learn MATLAB matrix manipulation techniques to optimize code efficiency
  • Explore MATLAB's built-in functions for polynomial evaluation
  • Study the use of symbolic math in MATLAB for solving systems of equations
  • Investigate debugging techniques in MATLAB to troubleshoot indexing errors
USEFUL FOR

Students and professionals in aerospace engineering, MATLAB programmers, and anyone involved in computational modeling of airfoil geometries.

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 ...
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K