Function that requires 2 variables
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have two variables, t and h, that are composed of 30831 individual elements (so there is a t1,t2,t3...;h1,h2,h3...) and I need to implement the following function:
h2 = h1(t2-t1)
h3 = h2(t3-t2) + h2
h4 = h3(t4-t3) + h3
any help would be greatly appreciated!
2 Kommentare
Antworten (2)
Image Analyst
am 4 Jul. 2016
Bearbeitet: Image Analyst
am 4 Jul. 2016
If h1, h2, etc. are really h(1), h(2), etc. and are elements of the 30831 element h, and assuming t1 through t4 are really t(1) through t(4), you just need to put in a multiplication symbol and parentheses:
h(2) = h(1) * (t(2)-t(1))
h(3) = h(2) * (t(3)-t(2)) + h(2)
h(4) = h(3) * (t(4)-t(3)) + h(3)
Or in a loop
for k = 2 : length(h)
h(k) = h(k-1) * (t(k) - t(k-1))
% Only add in an h element for index 3 and beyond.
if k >= 3
h(k) = h(k) + h(k-1)
end
end
Not sure why h(2) didn't have h(1) added in, but that's what you put.
What you have to decide is if you want it recursive or not. In other words, in the last equation, is h(4) calculated with the original h(3) OR the new h(3) that it just computed?
4 Kommentare
Walter Roberson
am 4 Jul. 2016
If your h1 is zero then it is easy to prove that all your other h must be 0. Look at
h(2) = h(1) * (t(2)-t(1))
with h(1) starting at 0, that will be 0 multiplied by something, which is going to give 0. So h(2) is going to give 0. Then you have
h(3) = h(2) * (t(3)-t(2)) + h(2)
which is 0 multiplied by something, plus 0. So h(3) will have to be 0. And then when h(3) is used that is going to be 0 multiplied by something plus 0, so h(4) is going to be 0; and so on. All your outputs must be 0 if your h(1) is 0. It is a recurrence relationship.
Walter Roberson
am 4 Jul. 2016
This can be reformulated like a recurrence formula:
syms k0 k
h(k) = (symprod(t(k0+1)-t(k0)+1, k0, 2, k-1))*h(1)*(t(2)-t(1))
except that in practice you cannot use symprod to index variables. Fortunately you can rework it:
h(2:length(t)+1) = cumprod( diff(t(2:end)) + 1 ) * h(1) * (t(2) - t(1))
Are you sure, though, that you want to overwrite all members of h except the first? Why bother having the other 30830 values for it when you start?
Siehe auch
Kategorien
Mehr zu Matrix Indexing 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!