MATLAB: Misbehaving script

functions

Hi, i'm writing a script to calculate diameters of pipes for me. However, Matlab is acting up on me (or i'm missing something).
I've set up a few parameters at the start of the script, defined a function and set up a for loop to get me my values.
Q_in=(2/9);
n_nozzle=10;
n_row=9;
d_in=250e-3;
Q_nozzle=2.2e-3;
Q=zeros(n_row,n_nozzle);
Q(1,1)=Q_in;
Q_R=@(r,c) Q(r-1,c)-(Q_nozzle+(n_nozzle*Q_nozzle))
for (i=2:n_row)
Q(i,1)=Q_R(i,1);
end
for the first iteration where i=2, the script works properly and produces the right answer. But starting from i=3, the Q(r-1,c) part of the Q_R function just keeps ending up as 0 where it should be referencing Q(2,1).
I have manually tried Q(2,1)-(Q_nozzle+(n_nozzle*Q_nozzle)) and in this case the Q(2,1) correctly references Q(2,1). The function just does not work properly if I use Q_R(3,1).
What is going on here ?

Best Answer

In your function handle, the "Q" that it is referencing is the original Q at the time you created the function handle. It is not linked with whatever is in your workspace.
The following should make this clear.
A = 10;
F = @(x) A + x
F(7)
A = 0;
F(7)
F = @(x) A + x
F(7)
Even though I change A, the function handle does not reflect that change until I redefine the function handle.