MATLAB Simple matlab interpolation help

AI Thread Summary
The discussion revolves around a function f(x) that increases with x and the implementation of a for loop combined with an if statement to generate a sequence of values. The initial values are set for x(1), x(2), and x(3), with a loop intended to calculate subsequent values based on the condition of f(x). The user encountered errors in calculating values beyond x(4) but later resolved the issue by recognizing the importance of the order of intervals in the calculations. This highlights the significance of correctly managing the sequence and conditions in iterative functions.
JXPES
Messages
2
Reaction score
0
suppose I have a function f(x) which I know increases as x.

I'm trying to combine a for loop and if statement

x(1)=a;
x(2)=b;
x(3)=(a+b)/2;

for i=3:n
if f(x(i)) > A
x(i+1) = (x(i-2)+x(i))/2;
else
x(i+1) = (x(i-1)+x(i))/2
end
end

this method works for x(4), the rest come with errors! Am I missing something obvious? Thanks.
 
Physics news on Phys.org
never mind. worked it out- the order of the intervals had to be considered
 

Similar threads

Back
Top