why do i keep getting attempt to acess xx; index must be a positive integer or logical?

function ni = newtonsinterp %f(a) calculates f at a given point "a" through the orders 1 to 3 % f(a) is calculated using Newtons interpolation %Input: % x = x distance % f(x) = function related to the distance x % subscripts of x and f(x) relate to given co ordinate (1 through 4) %Output: % f(a) is the unknown function value at the specified point % subscripts of f(a) pretain to the order of f(a)
a = 3.4;
x1 = 2.5; x2 = 3; x3 = 4; x4 = 5; % these points were chosen because the specified x vlaue given is 3.4 which % is inbetween 3 and 4 thus those two points plus one behind each was % selected to make the third order possible.
f(x1)=6.5; f(x2)=7; f(x3)=3; f(x4)=1;
%first orders are all calculated to find values to use with second order %equations
f(x2,x1) = ((f(x2)-f(x1))/(x2-x1)); f(x3,x2) = ((f(x3)-f(x1))/(x3-x2)); f(x4,x3) = ((f(x4)-f(x3))/(x4-x3));
%now, using the first oders solved above, the second orders can be %calculated
f(x3,x2,x1) = ((f(x3,x2)-f(x2,x1))/(x3-x1)); f(x4,x3,x2) = ((f(x4,x3)-f(x3,x2))/(x4-x2));
%using the second orders calculated above, the third order is not found
f(x4,x3,x2,x1) = ((f(x4,x3,x2)-f(x3,x2,x1))/(x4-x1));
%Now using the calculated orders above, the orders 1 through 3 can be %calculated for the specified point "a"
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);
f(a) = f1(a); f2(a); f3(a);
The error message I receive is:
??? Attempted to access f(2.5); index must be a positive integer or logical.
Error in ==> newtonsinterp at 22 f(x1)=6.5;

Answers (2)

I don't think it knows that "f" is a function. I think it thinks it's an array because it's not seeing your function declaration for f. That's my guess, not having seen the actual error message because you withheld it. Post the actual error message - ALL the red text -- if you want a better answer.

3 Comments

sorry that was my mfile the error message I receive is: ??? Attempted to access f(2.5); index must be a positive integer or logical.
Error in ==> newtonsinterp at 22 f(x1)=6.5;
Yep. It thinks f is an array and you're trying to access the 2.5th element of it, which you can do because indexes must be integers starting at 1, or logical values. Make sure you define f. Where did you do that? Where is the line that looks something like
function output = f(input)
or an anonymous function with an @ symbol?
ok thanks! would I define f between defining x and f(x) ?

Sign in to comment.

so I defined f as
function output = f(input) and a new error message comes up that reads:
??? Error: File: newtonsinterp.m Line: 57 Column: 1 The function "f" was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file.
however in my mfile, there is no line 57, it "ends" at line 55

3 Comments

If one function ends with an "end" statement, then they ALL have to end with an end statement. You can't have some end with "end" and some just end with nothing. For what it's worth, I never end my function definitions with an "end" statement.
if I delete the end statement, when running the file it does nothing.. I put an end statement at the end of the first set of functions I wish to compute. however there are several more sets of functions I wish to compute. Do I have to set a
function output = f(input) for each set?
(The whole point of this is to set up a generic file for solving any system of equations using Newton's Interpolation of polynomials..)
If the polynomial is different then you'd need different functions for every equation. Look up how to define a function and figure out what "end"s you need to use.

Sign in to comment.

Categories

Asked:

on 19 Feb 2014

Commented:

on 19 Feb 2014

Community Treasure Hunt

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

Start Hunting!