输入数组错误消息

时间:2017-02-18 18:34:20

标签: matlab

我正在尝试获得N的列表,这将是[8 16 32 64 128 256 512 1024],但是当我输入数组时,我得到'大小输入必须是标量'代码的零部分出错。

function  Euler_second_order

a = input('Enter your a = '); b  =input('Enter your b = '); 
y0 = input('Enter y(t0) = '); N = input('Enter your N or Ns: ');

fprintf('        \nThe Second-Order Euler''s method\n')

for n = 1:numel(N) %iterate through the list of N
w = zeros(N+1,1); t = zeros(N+1,1);

w(1)= y0;

t(1) = a;
h = (b-a)/N; 

max = 0;
for k = 1:N     %suppose to go from 1 to the current N
  w(k+1) = w(k) + h*f(t(k),w(k)) + ((h^2)/2)*(Fprimet(t(k),w(k)) +      Fprimey(t(k)) * f(t(k),w(k)));
t(k+1) = t(k) + h;
    p = w(k) - Y(t(k)); 
    if max < abs(p) 
        max = abs(p); 
    end

  end
 fprintf('%5d %12.8f\n',N, max) %prints the N array
 end

function dydt = f(t,y)
dydt = y*(1 + exp(2*t));
end

function yexact = Y(t)
yexact = exp(t+(exp(2*t)-1)/2);
end

function dfdt = Fprimet(t,y) %derivative with respect to t
dfdt = 2*y*exp(2*t);
end

function dfdy = Fprimey(t) %derivative with respect to t
dfdy = 1 + exp(2*t);
end

end

1 个答案:

答案 0 :(得分:0)

您在整个函数体中使用大写N而不是迭代变量n。因此,您的主要功能将最终为:

for n = 1:numel(N) %iterate through the list of N
w = zeros(n+1,1); t = zeros(n+1,1);

w(1)= y0;

t(1) = a;
h = (b-a)/n;

max = 0;
for k = 1:n     %suppose to go from 1 to the current N
    w(k+1) = w(k) + h*f(t(k),w(k)) + ((h^2)/2)*(Fprimet(t(k),w(k)) +      Fprimey(t(k)) * f(t(k),w(k)));
    t(k+1) = t(k) + h;
    p = w(k) - Y(t(k));
    if max < abs(p)
        max = abs(p);
    end

end
fprintf('%5d %12.8f\n',n, max) %prints the N array
end