why do i keep getting attempt to acess xx; index must be a positive integer or logical?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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;
0 comentarios
Respuestas (2)
Image Analyst
el 19 de Feb. de 2014
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 comentarios
Image Analyst
el 19 de Feb. de 2014
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?
steve
el 19 de Feb. de 2014
3 comentarios
Image Analyst
el 19 de Feb. de 2014
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.
Ver también
Categorías
Más información sobre Polynomials en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!