How to use if statement in function?

How do I define that my N needs to be >0. I tried if but I get error like
Output argument "prvi" (and maybe others) not assigned during call to "func1".
here is my writen code
function prvi = func1(x, N)
for k=1:N
prvi=(1-(4*x^2)/(pi^2*(2*k-1)^2))
end
end

 Respuesta aceptada

James Tursa
James Tursa el 9 de Abr. de 2021
Editada: James Tursa el 9 de Abr. de 2021
Add this code to the top of your function:
if( N <= 0 )
error('N needs to be > 0')
end
That being said, your loop probably doesn't do what you intended since it only assigns prvi based on the last iteration. Also, there are many other checks you can make on x and N to make sure they are the correct type, size, etc.

2 comentarios

Cris LaPierre
Cris LaPierre el 9 de Abr. de 2021
You could also look into Function Argument Validation.
Roko Serdar
Roko Serdar el 9 de Abr. de 2021
thank you guys

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Abr. de 2021

Comentada:

el 9 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by