error sign not valid

4 ビュー (過去 30 日間)
Charlene
Charlene 2013 年 5 月 9 日
I have this function
n = 1;
Product = 1;
while( Product < N )
{
Product = Product * (2*n);
n = n + 1;
}
end
and this error keeps on popping up Error: File: Untitled5.m Line: 5 Column: 9 The expression to the left of the equals sign is not a valid target for an assignment.
  1 件のコメント
Jordan Monthei
Jordan Monthei 2013 年 5 月 9 日
編集済み: Jordan Monthei 2013 年 5 月 9 日
For readability sake, I'm going to add this on here.
% this loop is used to find how many iterations are needed in the expression 2*4*6*...*2n in order to exceed a predetermined maximum.
n=1;
Product = 1; % variables initialized as one
while ( Product < N) % N being a predetermined maximum
{
Product = Product * (2 * n); % 2*4*6*...*2n until
n = n + 1;
}
end
it would be helpful to know what the rest of the program is doing outside of this function. Is N declared, if so, what is it declared as? Where does Line: 5 Column: 9 correspond within what you have?

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2013 年 5 月 9 日
That's not valid MATLAB syntax. In particular, the curly brackets shouldn't enclose the while loop. (You also used capital N where you had been using small n.)
Does this do what you intend?
n = 1;
Product = 1;
while( Product < n )
Product = Product * (2*n);
n = n + 1;
end
  2 件のコメント
Jordan Monthei
Jordan Monthei 2013 年 5 月 9 日
編集済み: Jordan Monthei 2013 年 5 月 9 日
small n is treated as a multiplier onto the 2 whereas large N is used as a maximum value. with that change, you have the intended function. Sorry about the curly brackets, that was my suggestion, I'm used to C coding.
Jordan Monthei
Jordan Monthei 2013 年 5 月 9 日
i ran this function in MATLAB with N=500; i received a value of 6 for n and a final product of 3840 showing that this function does work.
2 = 2 n=1 -> 1st iteration
2*4 = 8 n=2 -> 2nd iteration
2*4*6 = 48 n=3 -> 3rd iteration
2*4*6*8 = 384 n=4 -> 4th iteration
2*4*6*8*10 = 3840 n=5 -> 5th iteration
and then n is incremented one more time before exiting the loop to give n=6.
In this case you could use the expression "#iterations = n-1" after the loop and you would have your answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunctional Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by