0

I am trying to make a function to solve an equation using Eulers with multiple step sizes. When I go to the command window and input part2a(1.0,0.5,0.1,0.05) I get the error message:

Subscript indices must either be real positive integers or logicals.

Error in part2a (line 10)

f(t(j),y(j))=(7.2.*(sin(t(j))).^2)-0.208333.*(1+y(j)).^1.5;

I understand people have asked similar questions but I haven't been able to get any of their answers to work.

function part2a(h1,h2,h3,h4)
t=0;
tf=10;
n1=(tf-t)/h1;
n2=(tf-t)/h2;
n3=(tf-t)/h3;
n4=(tf-t)/h4;
y=0;
for j=1:n1
    f(t(j),y(j))=(7.2.*(sin(t(j))).^2)-0.208333.*(1+y(j)).^1.5;
    x=f(t(j),y(j));
    t(j+1)=t(j)+h1;
    y1(j+1)=y(j)+h1*x;
end
for j=1:n2
    f(t(j),y(j))=(7.2.*(sin(t(j))).^2)-0.208333.*(1+y(j)).^1.5;
    x=f(t(j),y(j));
    t(j+1)=t(j)+h2;
    y2(j+1)=y(j)+h2*x;
end
for j=1:n3
    f(t(j),y(j))=(7.2.*(sin(t(j))).^2)-0.208333.*(1+y(j)).^1.5;
    x=f(t(j),y(j));
    t(j+1)=t(j)+h3;
    y3(j+1)=y(j)+h3*x;
end
for j=1:n4
    f(t(j),y(j))=(7.2.*(sin(t(j))).^2)-0.208333.*(1+y(j)).^1.5;
    x=f(t(j),y(j));
    t(j+1)=t(j)+h4;
    y4(j+1)=y(j)+h4*x;
end
t1=0:h1:10;
t2=0:h2:10;
t3=0:h3:10;
t4=0:h4:10;
plot(t1,y1,t2,y2,t3,y3,t4,y4);
xlabel('Time in days');
ylabel('Change in depth of the tank in meters');
end
EBH
  • 10,350
  • 3
  • 34
  • 59
B. Lott
  • 3
  • 2
  • 1
    Start here: http://stackoverflow.com/q/20054047/2627163 – EBH Nov 01 '16 at 17:50
  • what is t(j) suppose to do? remember your t is just a scalar – Sason Torosean Nov 01 '16 at 17:54
  • t(j) in the f(t(j),y(j) was just supposed to be a generic equation. I changed it and y(j) back in the code. It was just something that I tried to fix the code. I'm a novice with MATLAB so any help is appreciated guys. – B. Lott Nov 01 '16 at 18:19

0 Answers0