Using functions to create a function
9 次查看(过去 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!