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 ?

 채택된 답변

Teja Muppirala
Teja Muppirala 2011년 12월 4일

1 개 추천

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.

댓글 수: 1

Paulo Silva
Paulo Silva 2011년 12월 4일
That was a tricky one, thanks for finding and explaining the error, +1 vote

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paulo Silva
Paulo Silva 2011년 12월 3일

0 개 추천

I see it working properly, see it by yourself with this code
clc
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);
i
Q_R(i,1)
pause(0.3)
end

댓글 수: 5

Christopher
Christopher 2011년 12월 3일
When I run the code Q(1,1) is 2/9 as I defined.
Q(2,1)=0.1980 as it should be
but Q(3,1)=-0.0242 which is wrong.
It should be doing 0.1980-0.0242 = 0.1738
But instead it is doing 0-0.0242=-0.0242
Paulo Silva
Paulo Silva 2011년 12월 3일
ok you are right and I'm stuck without a solution, it's been a while since I had this kinds of problems to solve but I won't give up
Paulo Silva
Paulo Silva 2011년 12월 3일
So it works diferently without the function
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;
for n=2:n_row
Q(n,1)=Q(n-1,1)-(Q_nozzle+(n_nozzle*Q_nozzle));
end
Q(1:end,1)
Christopher
Christopher 2011년 12월 3일
Yeah, i've already changed my code to work just like you have done. But it is still puzzling me why it does not work with the function ?
Paulo Silva
Paulo Silva 2011년 12월 3일
so am I, been away from such weird problems for some time, you can reproduce the original problem without the loop
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;
%for
n=2:n_row;
Q(n,1)=Q(n-1,1)-(Q_nozzle+(n_nozzle*Q_nozzle));
%end
Q(1:end,1)
very strange or I might be missing something simple :S

댓글을 달려면 로그인하십시오.

카테고리

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by