Here I am trying to do the Cobweb diagram for Tent function through MATLAB, and here is the code:
function cobweb(f,a,b,x0,N)
x0 = 0.8; % initial point
N = 200; % number of iterations
x = linspace(0,1,1000); % space of x
% define the Tent function
for i=1:numel(x)
if x(i) = 1/2
f(i) = 1.5*(1-x(i));
end
end
hold on
plot(x,f,'k')
hold on
plot(x,x,'r')
xlabel('x(t)')
ylabel('f(t+1)')
grid on
x(1) = x0;
line([x(1),x(1)],[0,f(x(1))]);
line([x(1),f(x(1))],[f(x(1)),f(x(1))]);
for i=1:N
x(i+1) = f(x(i));
plot([x(i+1),x(i+1)],[x(i+1),f(x(i+1))]);
hold on
plot([x(i+1),f(x(i+1))],[f(x(i+1)),f(x(i+1))]);
end
hold off
My question is, when I am running the code, MATLAB says:
Array indices must be positive integers or logical values.
Error in cobwebplot (line 31)
line([x(1),x(1)],[0,f(x(1))]);
But I don't understand why here it says so, because I didn't find any non-integer indices here, and can someone help me explain why here it has such error? Thanks!
if x(i) = 1/2which should at least beif x(i) == 1/2but even under this form , I don't understand how you obtain a tent. I advise you to use a closed form expression for the tent function such as $f(x)=1-abs(abs(x)-abs(x-1))$. – Jean Marie Feb 25 '24 at 23:15