First time with matlab, inputting cell arrays into function?

In summary: Thanks again!In summary, the conversation discusses a function called "viscosity" that takes input from a 3x4 matrix and calculates the Reynolds number, which is then used to classify the flow of liquid as laminar, transitional, or turbulent. The function also uses a for loop to iterate through the matrix and a disp function to display the results. Other topics discussed include using the help and doc functions, saving the function file as the same name, and preallocating a vector to store the resulting values.
  • #1
darkxynx
4
0
Hi I am currently trying to program something that will accept a vector as input for one my function. However, this is my first time programming, and I am relying completely on the internet and the Chapra textbook for help.

This is what I have so far:

%Re=reynolds number
%p = density of the fluid
%v=mean fluid velocity
%D=diameter of the fluid
%u=fluid viscosity
function Re=viscosity(p,v,D,u)
p=input('p: ');
v=input('v: ');
D=input('D: ');
u=input('u: ');

Re=(p*v*D)/u;
if Re<2000
disp('The liquid is laminar')
elseif Re>=2000&Re<=4000
disp('The liquid is transition')
else
disp('The liquid is Turbulent')
end

However, we have to input the a chart of data, running the function only once. I did some researching and I could not find any examples. If anyone could point me in the right direction, I'd really appreciate it. My next attempt would be to somehow use index and for loop.
The chart of data we have is a 3x4.

On a side note, anyone know any good resources for coding with matlab?

Thanks!
 
Physics news on Phys.org
  • #2
%Re=reynolds number
%p = density of the fluid
%v=mean fluid velocity
%D=diameter of the fluid
%u=fluid viscosity
function Re=viscosity(p,v,D,u)

Instead, I'd do:

function Re=viscosity
%Re=reynolds number
%p = density of the fluid
%v=mean fluid velocity
%D=diameter of the fluid
%u=fluid viscosity
(blank line here)

Then when ever you type "help Re" into the command prompt, you'll get all the commented lines up to the blank line. The function call always goes at the top.

p=input('p: ');
v=input('v: ');
D=input('D: ');
u=input('u: ');

This is unnecessary. Your function is:

Re=viscosity(p,v,D,u)

so just type this in the command prompt and replace the p,v,D,and u with the actual values. Of course, if your input is going to be a matrix that represents those values, then you can just do:

Re=viscosity(M)

where M is the matrix of three samples of p,v,D,u (3x4 if I interpret your matrix input correctly).

Re=(p*v*D)/u;
if Re<2000
disp('The liquid is laminar')
elseif Re>=2000&Re<=4000
disp('The liquid is transition')
else
disp('The liquid is Turbulent')
end

if you're doing a matrix, you'll have to:
a) use the size function to analyze the size of the matrix OR assume it's always 4x3.
b) use the size values (namely, the one that represents the sample size) to start a for loop that steps through the sample number. It will look something like this:

[a,b] = size(M) %assume b is the number of samples
Re = zeros(1,b) %preallocate a vector full of zeros for the Re value of each sample (experiment)

for i = 1:b %for each sample

p = M(1,i) %set the values from that sample
V = M(2,i)
... (etc) ... %you can fill in the blanks

Re(i) = p*V*d/u %compute Re for the ith sample

end

On a side note, anyone know any good resources for coding with matlab?

you can always do
'help [f]'
where [f]is a function you're curious about
'doc [f]'
will bring up the more detailed documentation

mathworks site has a forum with a lot of solutions.
 
  • #3
hey thanks for the help. I understood everything up to the part where you began selecting the matrix for input.

I think I got it though. So basically, using the size function, i get the number of columns,which is a set of data. With the for loop, it runs through each row, with each indexes, which is basically each column value?

Will the results for Re be a single value or will it return an answer in a matrix?

Thanks for your help man, cleared up a lot of things for me!
 
  • #4
With the for loop, it runs through each row, with each indexes, which is basically each column value?

Yes.

M(i,j) represent a value in the matrix with address: ith row, jth column


Will the results for Re be a single value or will it return an answer in a matrix?

Re will be a vector of the resulting value, u.

The way I wrote it, we collapse each column (i), which contain p V and d, into a single value, u.
 
  • #5
Hm, so I defined M as a 4x3 matrix, and then I tried to call the function. Is there something wrong with the code? This is the error message I get.

Re=viscosity(M)
? Undefined function or method 'viscosity' for input arguments of type 'double'.
 
  • #6
darkxynx said:
Hm, so I defined M as a 4x3 matrix, and then I tried to call the function. Is there something wrong with the code? This is the error message I get.

Re=viscosity(M)
? Undefined function or method 'viscosity' for input arguments of type 'double'.

my first guess is you haven't properly saved the function called "viscocity"

1) t must be saved as viscocity.m
2) it must have "function Re=viscosity(M)" as the first line.
3) it must be in your path directory for Matlab
 
  • #7
Yep it worked, thanks man.

Iwas just going through the code to make sure I understood it and I came across this line

Re = zeros(1,b) %preallocate a vector full of zeros for the Re value of each sample (experiment)

Why do we have to preallocate a vector for the values that we're going to calculate for anyway?

And basically, the for loop makes the program select the variable values for each index, then runs through the formula, and then loops back up and works on the next index?

If at the end of each loop, during the classification with the disp line, how would i say
"The liquid is Laminar for the ith sample" ?

I tried just sticking an i inside..that didn't quite work out

Thanks for all your help!

EDIT: Wait, so whenever I have function fout=nane(input)

the name always has to be file name? Is this just convention or better for calling purposes?
 
Last edited:
  • #8
darkxynx said:
Why do we have to preallocate a vector for the values that we're going to calculate for anyway?

It's a memory management thing. It his to create a new vector for each iteration in the loop if the vector grows. It's not necessary, but it's a lot faster if you preallocate. Some algorithms (searching and optimizing) don't have a set outcome length, so you can't preallocate (unless you estimate it... but then you have more difficulties of where the data ends and the 0's begin and you won't be able to determine your length if you need that for other processing actions).

And basically, the for loop makes the program select the variable values for each index, then runs through the formula, and then loops back up and works on the next index?

yup!

If at the end of each loop, during the classification with the disp line, how would i say
"The liquid is Laminar for the ith sample" ?

disp(['blah blah' num2str(i) ' blah blah'])

num2str converts the number "1" into the string "1". Otherwise you'll get ascii values (which you don't want).

EDIT: Wait, so whenever I have function fout=nane(input)

the name always has to be file name? Is this just convention or better for calling purposes?

calling purposes, I believe. Matlab looks for the file name. Scripts work the same way too, except scripts don't have a "function blah blah" reference at the top. So if I write a script file that just goes:

x = 5+10

disp(x)

and save it as scriptfile.m

then every time I enter the command 'scriptfile' into the command line, it will execute those lines of code.
 

Related to First time with matlab, inputting cell arrays into function?

1. How do I input a cell array into a function in Matlab?

To input a cell array into a function in Matlab, you can use the curly braces ( { } ) to enclose the cell array. For example, if your cell array is named "myArray", you can input it into a function as {myArray}.

2. Can I input multiple cell arrays into a function in Matlab?

Yes, you can input multiple cell arrays into a function in Matlab by separating them with commas inside the curly braces. For example, if you have two cell arrays named "myArray1" and "myArray2", you can input them into a function as {myArray1, myArray2}.

3. How do I access the elements of a cell array inside a function in Matlab?

To access the elements of a cell array inside a function in Matlab, you can use curly braces ( { } ) and the index number of the element you want to access. For example, if you want to access the first element of a cell array named "myArray", you can use {myArray{1}}.

4. Can I modify a cell array inside a function in Matlab?

Yes, you can modify a cell array inside a function in Matlab by using its index number and the assignment operator (=). For example, if you want to modify the second element of a cell array named "myArray", you can use myArray{2} = "new value".

5. How do I return a cell array from a function in Matlab?

To return a cell array from a function in Matlab, you can use the keyword "return" followed by the cell array you want to return. For example, if you want to return a cell array named "myArray" from a function, you can use "return myArray" at the end of the function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Classical Physics
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
Back
Top