# Function that requires 2 variables

1 visualizzazione (ultimi 30 giorni)
jgillis16 il 4 Lug 2016
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 CommentiMostra NessunoNascondi Nessuno
Walter Roberson il 4 Lug 2016
Is h1(t2-t1) multiplication or indexing?
jgillis16 il 4 Lug 2016
multiplication

Accedi per commentare.

### Risposte (2)

Image Analyst il 4 Lug 2016
Modificato: Image Analyst il 4 Lug 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 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
jgillis16 il 4 Lug 2016
I have never used it before (help?)
Walter Roberson il 4 Lug 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.

Accedi per commentare.

Walter Roberson il 4 Lug 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?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
jgillis16 il 4 Lug 2016
it's actually a quick and easy way for the first step of numerical integration. i'm supposed to repeat this process twice, first time through will get me h double dot to h dot, then the second time through will get me from h dot to h.

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by