why do i keep getting attempt to acess xx; index must be a positive integer or logical?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Image Analyst
2014-2-19
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 个评论
Image Analyst
2014-2-19
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
2014-2-19
3 个评论
Image Analyst
2014-2-19
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!