I keep getting a syntax error on the 'if' function.

5 ビュー (過去 30 日間)
Jacob Oleshchuk
Jacob Oleshchuk 2018 年 11 月 20 日
コメント済み: Jacob Oleshchuk 2018 年 11 月 20 日
function pieulgood
%approximates and prints pi with Euler's formula
%called as pieulgood or pieulgood()
%doesn't return a value, but a print
true=0;
n=0;
while true~=1
n=n+1;
for i=1:n
vec(i)=1/((i)^2);
end
approxpi=sqrt(6*sum(vec));
approxpirounded=str2double(fprintf('.6f\n',approxpi)
if approxpirounded==pi
true=1;
end
end
fprintf('A good approximation of pi, by Euler, with n=%d is %.6f\n',n,approxpi)
end
On the line "if approxpirounded==pi", my program tells this,
Please, can someone tell me what syntax is wrong here? I've used 'if' time and time again and never had this problem.
Edit: I fixed my code to coincide with MATLAB

採用された回答

Steven Lord
Steven Lord 2018 年 11 月 20 日
approxpirounded=str2double(fprintf('.6f\n',approxpi)
How many left parentheses ( are on that line? How many right parentheses )? Those numbers should be the same.
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 11 月 20 日
and the output of fprintf is the number of items converted, not the string.
Jacob Oleshchuk
Jacob Oleshchuk 2018 年 11 月 20 日
thanks, I changed it to a version of round.

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

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 11 月 20 日
That is not matlab code . matlab does not have endfor or endif or endwhile or endfunction . That appears to be octave code.

Mark Sherstan
Mark Sherstan 2018 年 11 月 20 日
I may be mistaken but I have not seen this type of syntax used before in MATLAB. Try changing your function to something like this:
function pieulgood
%approximates and prints pi with Euler's formula
%called as pieulgood or pieulgood()
%doesn't return a value, but a print
n=0;
while 1
n = n+1;
for i = 1:n
vec(i) = 1/((i)^2);
end
approxpi = sqrt(6*sum(vec));
%approxpirounded=str2double(fprintf('.6f\n',approxpi)
approxpirounded = round(approxpi,6)
if approxpirounded == pi
break
end
end
fprintf('A good approximation of pi, by Euler, with n=%d is %.6f\n',n,approxpi)
end
MATLAB stores true as a variable that should not be overwritten so I removed that. Use the round function not fprintf, use end not endif, endwhile, endfunction, etc... Also your function will most likely never end as pi in MATLAB is 3.141592653589793 and you are only comparing 6 decimal places. Please review the code changes and notes I made to gain a better understanding of the problems you are encountering, good job getting on the right track though.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 11 月 20 日
yes. the real line is the round afterwards
Jacob Oleshchuk
Jacob Oleshchuk 2018 年 11 月 20 日
alright, and thanks for the corrections with pi and true. You guys work fast on these and it helps a lot.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by