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

In summary, the conversation is about creating a code in MATLAB to describe the geometry of a PARSEC airfoil. The x coordinates for the upper and lower surfaces of the airfoil are given by a formula with a summation over n = 1 to n = 6. The coefficient a_n is determined by solving a system of linear equations. The approach to the problem involves creating a matrix with 6 columns and finding a vector of z-coordinates by summing the values across each column. However, there seems to be an issue with the size of the matrix and an index out of bounds error. The person giving advice suggests troubleshooting with smaller vectors and using matrix equations.
  • #1
HACRS4
3
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
  • #2
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 ...
 

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

1. What is a matrix and why would I want to create one?

A matrix is a grid of numbers or values arranged in rows and columns. It is commonly used in mathematics, statistics, and computer programming to store and manipulate data. Creating a matrix can help organize and process large sets of data efficiently.

2. How do I create a simple for loop in my code?

To create a for loop, you will need to specify a starting value, an ending value, and a step size. For example, a for loop that counts from 1 to 10 with a step size of 1 would look like this:
for(i = 1; i <= 10; i++) {
// code to be executed
}

3. Can I use a for loop to create a matrix with multiple dimensions?

Yes, you can use nested for loops to create a matrix with multiple dimensions. For example, to create a 3x3 matrix, you would use two for loops, one for the rows and one for the columns, and assign values to each element in the matrix.

4. How do I assign values to a matrix using a for loop?

You can assign values to a matrix using the index notation. For example, to assign the value 5 to the element in the first row and second column of a matrix named "myMatrix", you would use the following code:
myMatrix[0][1] = 5;

5. Are there any other methods besides for loops to create a matrix?

Yes, there are other methods to create a matrix, such as using built-in functions or libraries in your programming language. For example, in Python, you can use the NumPy library to create and manipulate matrices. However, for loops are a commonly used and versatile method for creating matrices in many programming languages.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
960
  • Engineering and Comp Sci Homework Help
Replies
6
Views
883
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top