Info

This question is closed. Reopen it to edit or answer.

can functions in matlab be related to other functions within the same mfile?

1 view (last 30 days)
I'm trying to set up a general mfile to calculate a required function when given a selected data point and a group of known x values and f(x) values using Newton's interpolation of polynomials of orders 1 through 3.
As per the equations for solving this, there are a lot of functions within other functions. Can matlab read and calculate these?
for example:
you have:
x1 = 2.5 x2 = 3 x3 = 4 x4 = 5
and the related f(x) values are:
f(x1) = 6.5 f(x2) = 7 f(x3) = 3 f(x4) = 1
and you want to interpolate for:
f(x)
when x = 3.4
in between x2 and x3 above
setting x = 3.4 (given value to solve) as:
a = 3.4
to change the variable x allowing it to be used below
the first set of equations are as follows, which relate to x and f(x) values:
Eq.1) f(x2,x1) = ((f(x2)-f(x1))/(x2-x1))
Eq.2) f(x3,x2) = ((f(x3)-f(x2))/(x3-x2))
Eq.3) f(x4,x3) = ((f(x4)-f(x3))/(x4-x3))
For the second set of equations, I need to relate to Eq.1 (f(x2,x1), Eq.2 (f(x3,x2) and Eq.3 f(x4,x3).
Is it possible for me to write this for the second set of equations:
Eq.4) f(x3,x2,x1) = ((f(x3,x2)-f(x2,x1))/(x3-x1))
Eq.5) f(x4,x3,x2) = ((f(x4,x3)-f(x3,x2))/(x4-x2))
and the third set of equations, relating to the second set of equations (Eq.4 and Eq.5) as:
Eq.6) f(x4,x3,x2,x1) = ((f(x4,x3,x2)-f(x3,x2,x1))/(x4-x1))
Using Eq's 1 through 6, I can then solve for the desired order as follows (f1 being order 1, f2 being order 2 and f3 being order 3):
f1(a) = f(x1)+f(x2,x1)*(a-x1)
f2(a) = f1(a)+f(x3,x2,x1)*(a-x1)*(a-x2)
f3(a) = f2(a)+f(x4,x3,x2,x1)*(a-x1)*(a-x2)*(a-x3)
The whole purpose of this is to link all the equations together so that only the first4 x values and first 4 f(x) values need to be changed and the system will then interpolate the required info solving for f(a).
Is this possible? I have tried several different ways of creating this mfile however matlab doesn't seem to want to link the f(x1,x2) etc. together..
How can this be done?

Answers (0)

Community Treasure Hunt

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

Start Hunting!