I need to change this function to arrays!

2 次查看(过去 30 天)
Greetings I need to change the function below to arrays. Instructions: Implement the equation (Leibniz- estimate pi [1/1 − 1 /3 + 1 /5 − 1/ 7 + 1 /9 − ⋯ = π /4 ]) in a function that receives the relative error willing to accept (x) and return the user a vector with all values of π estimated(es) and other vector with relative errors associated with each value of π. (er). Thanks a lot.
function [es,er]=ejercicio2d(x)
RT=pi;
ter=0;
estimado=1;
er=2*x;
while er>=x
z=((1^(ter)+(ter+1))/(ter+1))^((-1)^ter);
estimado=(estimado*z);
es=estimado* 2;
er=100*abs((es-RT)/RT);
ter=ter+1;
end
end

采纳的回答

KSSV
KSSV 2016-10-19
function [es,er]=ejercicio2d(x)
RT=pi;
ter=0;
estimado=1;
er=2*x;
ER = [] ;
ES = [] ;
while er>=x
z=((1^(ter)+(ter+1))/(ter+1))^((-1)^ter);
estimado=(estimado*z);
es=estimado* 2;
er=100*abs((es-RT)/RT);
ter=ter+1;
ES(ter) = es ;
ER(ter) = er ;
end
end
If the loop length is known, can be initialized by dimension.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by