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

In summary, the conversation discusses finding y(ti) using an interpolation formula and how to write a code for it. The suggested code involves setting up y and using a loop to compute the next value of y using the previous value.
  • #1
sara_87
763
0

Homework Statement



If i have an interpolation formula, say:

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

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
  • #2
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
 
  • #3
Can anyone help me with this?



There are a few ways you can write a code to solve for y(ti) using the interpolation formula provided. Here is one possible approach:

function y = interpolate(a, b, n)

% Initialize h and ti
h = (a + b) / n;
ti = 0;

% Create an array to store y values
y = zeros(1, n+1);

% Use a for loop to iterate through each value of ti
for i = 1:n+1
% Use the interpolation formula to calculate y(ti)
y(i) = 3 + h * sum(1:i .* y(1:i));

% Update ti for the next iteration
ti = ti + h;
end

% Set y(t0) = 0
y(1) = 0;

% Plot the results
plot(t, y);
xlabel('t');
ylabel('y(t)');
title('Interpolation of y(t)');

This code creates a function called "interpolate" that takes in the values of a, b, and n as inputs. It then calculates h and initializes ti to 0. Next, it creates an array to store the values of y and uses a for loop to iterate through each value of ti, calculating y(ti) using the interpolation formula provided. The code also sets y(t0) = 0 and plots the results.

Keep in mind that this is just one possible approach and there may be other ways to write the code. It's always a good idea to test your code with different inputs to make sure it is working correctly.
 

1. What is interpolation?

Interpolation is the process of estimating values between known data points. In other words, it is the method of finding intermediate values by using known data points to fill in the gaps.

2. How does interpolation work in MATLAB?

In MATLAB, interpolation is performed by using various functions such as interp1, interp2, and interp3. These functions use different interpolation methods, such as linear, cubic, and spline, to interpolate data. These methods use mathematical equations to estimate the missing values based on the known data points.

3. What are some common uses of interpolation in MATLAB?

Interpolation is commonly used in MATLAB for data analysis and visualization. It is also used in image processing, signal processing, and curve fitting. It can be helpful in filling in missing data points or creating a smoother representation of a dataset.

4. Can interpolation be inaccurate?

Yes, interpolation can be inaccurate if the data points are not evenly spaced or if the interpolation method used is not appropriate for the data. It is important to carefully choose the interpolation method and evaluate the results to ensure accuracy.

5. Is there a limit to the number of data points that can be used for interpolation in MATLAB?

No, there is no limit to the number of data points that can be used for interpolation in MATLAB. However, using too many data points can result in a slower computation and may not necessarily improve the accuracy of the interpolation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
821
  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
1
Views
935
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
885
  • Engineering and Comp Sci Homework Help
Replies
1
Views
952
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Replies
5
Views
376
Back
Top