MATLAB: Is the code not working

wrong code

I need to plot a graph of error_d against a. a is changing by 10 units from 0 to 1000 so thats why I used a for loop. My code is the following: for a=1:10:1000 A_n=((-3)^-a)/(2*a+1); D_n= A_n*sqrt(12); error_d= abs(pi-D_n); axis([0 1000 0 1]); plot(a,error_d); end
but this is not showing any graph. I donot know what is wrong. Can anyone help me?

Best Answer

It is not showing a plot because ‘error_d’ is not in the range you believe it to be. Also, it is much more efficient to take advantage of MATLAB’s ability to vectorise expressions.
The Code
a=1:10:1000;
A_n=((-3).^-a)./(2*a+1);
D_n= A_n*sqrt(12);
error_d= abs(pi-D_n);
figure(1)
plot(a,error_d);
axis([0 1000 ylim])
This will produce the plot.