2021年4月6日星期二

Subscript indices must either be real positive integers or logicals - Matlab error

I am trying to do the euler method to solve the Initial Value problem using the matlab

    close all;   clear;   clc;    a=0; %initial limit  b=1.2;% final limit  n=20; %Number of points  h=(b-a)/n; %step length    t=a:h:b;  wn(i+1)=[0.5 zeros(1,n)];    for i=1:n  wn(i+1)=wn(i)+h*(wn(i)-t(i)^2)+1;  end    yActual=(t+1).^2-0.5*exp(t);    err=abs(wn-yActual);     disp('==============================================================================')  disp('   t_i              w_i                  y_i              |w_i-y_i| ')  disp('               (Approximation)      (Actual Value)         (Error) ')  disp('==============================================================================')      for i=1:n+1  fprintf('%0.5f \t %0.5f \t %0.5f \t %0.5f \n',t(i),wn(i),yActualI(i),err(i));  end    plot(t,wn,'-or','Linewidth',2)  hold on  gird on  plot(t,yActual,'-b','Linewidth',2)      xlabel('t')  ylabel('y')  legend('Euler Approximation','Actual Value')  

but when I run the code it shows me this error and I searched a lot and not finding a way to solve it:

Subscript indices must either be real positive integers or logicals.    Error in Assignment3_Q2 (line 11)  wn(i+1)=[0.5 zeros(1,n)];  

How I can solve it? please if you can show me by code because I am beginner in MatLab

https://stackoverflow.com/questions/66978276/subscript-indices-must-either-be-real-positive-integers-or-logicals-matlab-err April 07, 2021 at 08:58AM

没有评论:

发表评论