how to make a plot from my function results?
1 回表示 (過去 30 日間)
古いコメントを表示
How to make a plot from my function results?
(My function finds pi_aprox with a method and calculates the error from pi every time,it calculate for n times) i want to plot (error) and (n). From that plot we will see that inreasing n the error will be decreased.
2 件のコメント
Alireza Ghaderi
2020 年 5 月 13 日
simply create a variable called error... on each row store the new function output. then the row number is associated with the n.
then plot(error,n)
回答 (1 件)
the cyclist
2020 年 5 月 13 日
編集済み: the cyclist
2020 年 5 月 13 日
Here is an example using a for loop that calculates and plots the Gregory-Leibinz approximation to pi:
n_max = 100;
pi_approx = zeros(n_max,1);
pi_approx(1) = 4/1;
for n = 2:n_max
factor = (-1)^(n-1) / (2*n - 1);
pi_approx(n) = pi_approx(n-1) + 4*factor;
end
figure
plot(1:n_max,pi_approx - pi)
Here is an equivalent vectorized version:
n = 1:100;
factor = 4 * (-1).^(n-1) ./ (2*n - 1);
pi_approx = cumsum(factor);
figure
plot(1:n_max,pi_approx - pi)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Annotations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!