Using functions to create a function
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 25 Mär. 2013
Do not name a variable the same thing as your function name: there is too much chance of accidentally getting recursion.
Antworten (2)
Walter Roberson
am 25 Mär. 2013
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 Kommentare
Image Analyst
am 25 Mär. 2013
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 Kommentare
Siehe auch
Kategorien
Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!