How can I save all values for v_max in MATLAB when n ranges from 5 to 10?

In summary, the programmer wrote code that saves the values of a variable for six different values, but forgot to delete the operator for an array. When the code is run, an error is generated because the variable is stored as an array and the programmer forgot to include the operator for the array.
  • #1
mathsgeek
63
0
URGENT variable name MATLAB help

How do i change the variable, so it saves all the values, even if it must happen for 6 different variables name. This is because when i run it, it only saves the values for v_max when n=10 as this is the last iteration. How can i make all the values be saved. My coding is below:

radius=1:0.5:2.5;
total_flow_velocity=5

for n=5:1:10
v_max = (total_flow_velocity.*(1+n).*(1+(2.*n)))./((2.*pi)*((radius.*n).^2))
end

Thanks
 
Physics news on Phys.org
  • #2


probably the best way is to store them in a vector. You define a vector called v_max with a length equal to the amount of values, in your case that is 6:

v_max=[1:6];

then you can store each value in one of the positions of the maxtrix like:

for n=5:1:10
v_max(1,n-4) = (total_flow_velocity.*(1+n).*(1+(2.*n)))./((2.*pi)*((radius.*n).^2))
end

I used n-4 to define column number 1 because you start with n=5. After that you can recall any value by typing:

v_max(1,1) ... v_max(1,6)

good luck!
 
  • #3


ive tried it but i get the error "Subscripted assignment dimension mismatch." ? Also why did u do vmax=[1:6] because for each r value I've made it so i get an answer at each n value, so shouldn't the matrix be a 6x4?
 
Last edited:
  • #4


JeroenS' code has a mistake inside for loop. v_max(1,n-4) ==> indicate member (1, n-4) of array v_max. However, in the calculation, he forgot to delete the operator for array: (radius.*n). This operator (.*) will return an array radius multipled by n. So, in the left-side, you have 1 variable, not an array but in right side, there is an array. That's the reason MATLAB throws an error.

My code is:

Code:
vM = [];
j = 0;

radius = 1 : 0.5 : 2.5;
tfv = 5;

for n = 5 : 1 : 10
    j = j + 1;
    nR = 2 * pi * (radius * n).^2;
    vM(j, :) = (1 + n) * (1 + 2*n) * tfv ./ nR;
end

Hope this help.

p/s: i am totally confused when i read your code. There are many unnecessary parentheses and operators.
 
  • #5


Thanks so much, yeh i don't know why i use so many brackets and that, buts it because I am use to the calculator and have only been learning MATLAB for a few weeks now :P but I am beginning to learn to use less brackets :P
 

1. What is a variable in Matlab?

A variable in Matlab is a symbolic name that represents a value or a set of values. It is used to store and manipulate data in a program.

2. How do I declare a variable in Matlab?

To declare a variable in Matlab, you simply need to assign a value to it using the equals sign (=). For example, to declare a variable named "x" with a value of 5, you would use the code: x = 5.

3. What are the rules for naming variables in Matlab?

There are a few rules to follow when naming variables in Matlab. Variable names must start with a letter, followed by any combination of letters, numbers, and underscores. They cannot contain spaces or special characters, and they are case-sensitive.

4. How do I change the value of a variable in Matlab?

You can change the value of a variable in Matlab by simply reassigning it using the equals sign (=). For example, if you want to change the value of "x" from 5 to 10, you would use the code: x = 10.

5. Can I use words like "if" or "for" as variable names in Matlab?

No, you cannot use keywords, such as "if" or "for", as variable names in Matlab. These words have special meanings in the language and are reserved for specific functions or operations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
931
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
741
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
992
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top