Interpolation Formula in MATLAB | Solving for y(ti) with Code Example

AI Thread Summary
The discussion focuses on implementing an interpolation formula in MATLAB to compute y(ti) based on a defined equation. The formula involves calculating h as (a+b)/n and using a loop to iterate through values of i from 1 to n+1. The user is attempting to set up a code structure that initializes an array for y values and uses previously computed y values to calculate the next y value. The key challenge is correctly referencing the previous y values within the nested loop to apply the interpolation formula. This approach emphasizes the importance of element-wise operations in MATLAB for accurate computations.
sara_87
Messages
748
Reaction score
0

Homework Statement



If i have an interpolation formula, say:

y(t_i)=3+h\sum^i_{j=1}jy(t_j)

where i=0 to n
h=(a+b)/n
ti=i*h
and
when i=0, we have: y(t0)=0.

How would i write a code to find y(ti)?

Homework Equations





The Attempt at a Solution



function y=examp(a,b,n)
h=(a+b)/n
for i=1:n+1
t(i)=(i-1)*h
for j=1:i+1

and now I am stuck because i don't know how write a code that uses the previous value of y to compute the next value of y.
 
Physics news on Phys.org
Before the first loop, I would set up the y's(actually, it would be faster for your code to init the t's to zeros to, but unless n is very large it shouldn't matter):

y = zeros(n+1,1)

Then in the loop it would be something like
y(j) = 3 + sum( y(1:j-1).*(1:j-1) )

the .* does an element-wise product
 

Similar threads

Back
Top