how to make a plot from my function results?

조회 수: 1 (최근 30일)
Chris ch
Chris ch 2020년 5월 13일
편집: the cyclist 2020년 5월 13일
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
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)
Chris ch
Chris ch 2020년 5월 13일
Ty for your answer , cause i am new in matlab ,how to write this ?

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

답변 (1개)

the cyclist
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)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by