Frage

I have a problem with matlab ode45. When I write the system y(1) and y(2) only it works but when I use more it sayes Attempted to access y(4); index out of bounds because numel(y)=3 Here is the code of my program, plz check it `

function ode_epidemic(  )
global alfa beta hama delta lambda myu
global nfunc
nfunc=0;
alfa=1; beta=1; hama=1; delta=1; lambda=1; myu=1;
options = odeset('RelTol',1e-5);
sol = ode45(@epidemic,[0 20],[5; 0.1; 0],options);
figure
hold on;
xlabel('x','FontSize',11)
ylabel('y','FontSize',11)
plot(sol.x,sol.y(1,:),'-',...
     sol.x,sol.y(2,:),'-.',...
     sol.x,sol.y(3,:),'.','LineWidth',2)
hold off;
grid 'on'
disp('number of iteration')
iter=length(sol.x)
disp('number calculation of F')
nfunc
iter1=iter-1;
h=zeros(iter1,1);
x=zeros(iter1,1);
for i=1:iter1
 h(i)=sol.x(i+1)-sol.x(i); 
 x(i)=sol.x(i);
end
figure
hold on;
xlabel('x');ylabel('y');
plot(x,h,'LineWidth',2);
hold off;
grid 'on'
end

function dy = epidemic(t,y)
global alfa beta hama delta lambda myu
global nfunc
nfunc=nfunc+1;
dy=zeros(4,1);
dy(1)=2*t*y(1)*y(4);
dy(2)=10*t*y(1)^5*y(4);
dy(3)=2*t*y(4);
dy(4)=-2*t*(y(3)-1);
end

`

War es hilfreich?

Lösung

The y vector you are supplying only has 3 elements.

Replace:

ode45(@epidemic,[0 20],[5; 0.1; 0;],options);

with

ode45(@epidemic,[0 20],[5; 0.1; 0; 0],options);

and the code runs without error.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top