How to use if statement in function?

조회 수: 3 (최근 30일)
Roko Serdar
Roko Serdar 2021년 4월 9일
댓글: Roko Serdar 2021년 4월 9일
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

채택된 답변

James Tursa
James Tursa 2021년 4월 9일
편집: James Tursa 2021년 4월 9일
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
Cris LaPierre
Cris LaPierre 2021년 4월 9일
You could also look into Function Argument Validation.
Roko Serdar
Roko Serdar 2021년 4월 9일
thank you guys

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by