Function that requires 2 variables

1 次查看(过去 30 天)
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 个)

Image Analyst
Image Analyst 2016-7-4
编辑:Image Analyst 2016-7-4
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 个评论
jgillis16
jgillis16 2016-7-4
I have never used it before (help?)
Walter Roberson
Walter Roberson 2016-7-4
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
Walter Roberson 2016-7-4
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 个评论
jgillis16
jgillis16 2016-7-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by