Is there any way to control the number of decimal places for the outputs?
古いコメントを表示
I am studying root finding problem in numerical analysis. I have written down MATLAB codes for Bisection and Newton methods. They are running fine but I need to have different number of decimal places in the output for different questions. But Matlab either give 4 or 15 decimal places with default and format long settings.
Is there any way to control the number of decimal places in the output through code or by any other mean?
1 件のコメント
Mathematics
2014 年 7 月 19 日
編集済み: Star Strider
2014 年 7 月 19 日
回答 (1 件)
Matz Johansson Bergström
2014 年 7 月 19 日
0 投票
I would use num2str(pi,5) to get a string of pi with 5 decimals, for instance.
7 件のコメント
Matz Johansson Bergström
2014 年 7 月 19 日
...or you could write something like fprintf(1, 'Approximation: %.5f\n', pi)
Matz Johansson Bergström
2014 年 7 月 19 日
So, I would put inside the loop
fprintf(1, '%.0f %.7f %.7f %.7f %.7f %.7f\n',iter,a,b,m,ym,bound)
Mathematics
2014 年 7 月 19 日
編集済み: Mathematics
2014 年 7 月 19 日
Matz Johansson Bergström
2014 年 7 月 19 日
Why disp(out)?
Matz Johansson Bergström
2014 年 7 月 19 日
編集済み: Matz Johansson Bergström
2014 年 7 月 19 日
If you want you could print out 'out' after the loop, I'm not sure you want to see the iterations as they progress, or if you want to see the results after the fact.
Anyway, to do that you need to transpose the 'out' matrix, so we now have
fprintf(1, '%.0f %.7f %.7f %.7f %.7f %.7f\n', out')
Observe, this is AFTER the loop. Additionally, the '\n' is a newline character, you need it.
Mathematics
2014 年 7 月 19 日
Mathematics
2014 年 7 月 19 日
カテゴリ
ヘルプ センター および File Exchange で Correlation and Convolution についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!