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

  • Context: MATLAB 
  • Thread starter Thread starter mathsgeek
  • Start date Start date
  • Tags Tags
    Matlab Variable
Click For Summary

Discussion Overview

The discussion revolves around how to save multiple values of the variable v_max in MATLAB when iterating over a range of n values from 5 to 10. Participants are exploring coding techniques and addressing errors encountered in the process.

Discussion Character

  • Technical explanation, Debate/contested, Homework-related

Main Points Raised

  • One participant seeks help on saving all values of v_max across iterations, noting that only the last value is retained.
  • Another participant suggests using a vector to store values, proposing a specific indexing method to save results for each n value.
  • A different participant reports an error related to "Subscripted assignment dimension mismatch" and questions the initial suggestion regarding the size of the matrix.
  • One participant critiques the previous code for a mistake in array handling and provides an alternative solution that initializes an empty matrix and populates it correctly.
  • A later reply expresses confusion over the original code's complexity, attributing it to the participant's inexperience with MATLAB syntax.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to save the values of v_max, as there are multiple proposed methods and some reported errors. The discussion remains unresolved regarding the optimal solution.

Contextual Notes

Participants mention issues with array dimensions and MATLAB syntax, indicating potential misunderstandings of how to handle array operations and indexing in MATLAB.

mathsgeek
Messages
63
Reaction score
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


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!
 


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:


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.
 


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
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K