Function that requires 2 variables

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 Comments

Is h1(t2-t1) multiplication or indexing?
multiplication

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 4 Jul 2016
Edited: Image Analyst on 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 Comments

Yeah, so this function will be for all 30831 elements of h. h1 is assumed to be zero, therefore I didn't include it in. and h4 is calculated using the new h3.
is it possible to shift the output into a new matrix or a text file? i want to save all 30831 new values of h
OH! You want to do integration. Why not just use cumtrapz()?
I have never used it before (help?)
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.

Sign in to comment.

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 Comment

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.

Sign in to comment.

Asked:

on 4 Jul 2016

Commented:

on 4 Jul 2016

Community Treasure Hunt

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

Start Hunting!