put it in a function

2 visualizaciones (últimos 30 días)
Mingxuan
Mingxuan el 11 de Nov. de 2022
Respondida: RAGHUNATHRAJU DASHARATHA el 11 de Nov. de 2022
How would I put the following code in a in script function with a function name calc_pi(n).
count = 0;
for i = 1:N
u = rand(1,2);
if u(1)^2+u(2)^2<=1
count = count+1;
end
end
pi_est = count/N*4
  1 comentario
Dyuman Joshi
Dyuman Joshi el 11 de Nov. de 2022
function pi_est = calc_pi(N)
%your code
end

Iniciar sesión para comentar.

Respuestas (1)

RAGHUNATHRAJU DASHARATHA
RAGHUNATHRAJU DASHARATHA el 11 de Nov. de 2022
As per my understanding you want to write your code into a function .
I will be demonstrating it using your code as example below.
a=calc_pi(10)
a = 2.8000
function pi_est=calc_pi(N)
count = 0;
for i = 1:N
u = rand(1,2);
if u(1)^2+u(2)^2<=1
count = count+1;
end
end
pi_est = count/N*4;
end
To know more about functions go through this link

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by