Using functions to create a function
조회 수: 7 (최근 30일)
이전 댓글 표시
I want to create 2 functions called ADX and ADXR (ADXR uses ADX to calculate itself). This is the function for ADX-
function [ adx ] = adx( t, p )
adx(i) = [adx(i-t)*(p-t)+DX)]/p
end
Is this the correct way to program the function for ADXR?
function [ adxr ] = adxr( p )
adxr(i) = [adx(i) + adx(i-p)]/2
end
Or does it need the input arguments of adx -"adx(t,p)"? If so, how do I tell matlab I want the previous adx value and not the current value?
function [ adxr ] = adxr( p )
adxr(i) = [adx(t, p) + adx(i-p)(t,p)]/2
end
댓글 수: 2
Walter Roberson
2013년 3월 25일
Do not name a variable the same thing as your function name: there is too much chance of accidentally getting recursion.
답변 (2개)
Walter Roberson
2013년 3월 25일
In your line
adx(i) = [adx(i-t)*(p-t)+DX)]/p
end
because "i" is not a parameter to the function and is not a variable you initialize, "i" is going to default to sqrt(-1) . If your code managed to get anywhere, it would fail because you cannot index an array at sqrt(-1)
댓글 수: 0
Image Analyst
2013년 3월 25일
Maybe something like this:
function adxrOutput = adxr( p, i )
adxrOutput = [adx(i) + adx(i-p)]/2
end
function adxOutput = adx(t, p, i)
adxOutput = [adxOutput(i-t)*(p-t)+DX)]/p
end
I'm not sure I follow what you're trying to do though, and in the adx function, it's still not going to work (even though I made the output variable separate from the function name), but I don't know how to fix it because you haven't said what you want to do.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!