How do i make multiple conditions for multiple variable

so i have a function for exampe h= help(n,x)
h= 2 * help(n-1,x) + help(n-2,x) which should work only when -3<x<3 and n>=2.
and i want it such that when the conditions isnt fulfilled, disp(error: x is outside intervall) and/or disp(error: n is smaller than 2)
so when only x is wrong, only 'error: x is outside intervall' shows.
when only n is wrong, only 'error: n is smaller than 2' shows.
but if both x and n are wrong, then both statements are shown.

답변 (1개)

KSSV
KSSV 2020년 6월 13일
편집: KSSV 2020년 6월 13일
if ~(( x > -3) & (x < 3))
error("Please check the value of x ")
end
if ~(n>=2)
error("Please check the value of n")
end
end

댓글 수: 3

but then when both n and x are wrong, only 'Please check the value of x' is given.
I need it such that when both conditions are not met, both reasons/errors are given.
you are right.....now check..you can extend the code youself.
but if i do it this way,
function h= help(n,x)
if ~(( x > -3) & (x < 3))
error("Please check the value of x ")
end
if ~(n>=2)
error("Please check the value of n")
else
h= 2 * help(n-1,x) + help(n-2,x)
end
end
when x condition is not met, but condition of n is, it shows: 'Please check the value of x' and the answer.
I need it such that, when both conditions are not met, both error are shown.
when only one condition is not met, one of the error is shown, but the equation is not calculated.
when both condition is not met, the equeation is calculated.

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

질문:

2020년 6월 13일

댓글:

2020년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by