【问题标题】:Unable to perform assignment because the size of the left side is 1-by-2 and the size of the right side is 2-by-2无法执行分配,因为左侧的大小是 1×2,右侧的大小是 2×2
【发布时间】:2020-04-16 16:38:21
【问题描述】:

代码尝试实现欧拉方法并将欧拉方法改进为二阶微分方程,但存储 y 和 dy 值的 ys 值数组存在维度错误

f=@(x,y) [y(2); (2/x)*y(2)-(2/x^2)*y(1)];     % function through a function handle
x0 = 1; y0 = [4,9]; xf=2;                          % IC

    % Improve and Euler's Method
    [xs,ys] = ode45(f,[x0,xf],y0);
    [xsi1,ysi1] = Ieuler(f,[x0,xf],y0,0.1);
    [xse1,yse1] = euler(f,[x0,xf],y0,0.1);
    [xsi2,ysi2] = Ieuler(f,[x0,xf],y0,0.5);
    [xse2,yse2] = euler(f,[x0,xf],y0,0.5);
    
    % plotting all solutions
    plot(xsi1,ysi1(:,1),'-b','LineWidth',1.5); hold on;
    plot(xse1,yse1(:,1),'-r','LineWidth',1.5); hold on;
    plot(xsi2,ysi2(:,1),'-g','LineWidth',1.5); hold on;
    plot(xse2,yse2(:,1),'-k','LineWidth',1.5); hold on;
    plot(xs,ys(:,1),'-b','LineWidth',1.5); hold on;
    axis([0 xf -0.1 4]); xlabel('x');ylabel('y')

代码的第二部分是实现欧拉方法的构造函数

%Function calls
% function: Euler's Method implementation
function [xs,ys] = euler(f,xv,y0,h)
  x0 = xv(1); X = xv(2);
  N = (X-x0)/h;
  xs = zeros(N+1,1); ys = zeros(N+1,length(y0));
  x = x0; y = y0;
  xs(1) = x; ys(1,:) = y';
  for i = 1:N
      s1 = f(x,y);                  %evaluate direction field at current point
      y= y+s1*h;                    %find new y
      x = x+h;
      xs(i+1) = x; ys(i+1,:) = y';  %store y(1), y(2) in a row array
  end
end
% function: Improved Euler's Method implementation
function [xs,ys] = Ieuler(f,xv,y0,h)
  x0 = xv(1); X = xv(2);
  N = (X-x0)/h;
  xs = zeros(N+1,1); ys = zeros(N+1,length(y0));
  x = x0; y = y0;
  xs(1) = x; ys(1,:) = y';
  for i = 1:N
      s1 = f(x,y);                  %evaluate direction field at current point
      yE= y+s1*h;                   %find Euler value yE
      s2 = f(x+h,yE);               %evalute direction field at Euler point
      y = y + h*((s1+s2)/2);        *%find new y*
      x = x+h;
      xs(i+1) = x; ys(i+1,:) = y';  *%store y(1), y(2) in a row array* 
  end
end
Unable to perform assignment because the size of the left side is 1-by-2 and the
size of the right side is 2-by-2.

Error in Untitled>Ieuler (line 63)
      xs(i) = x; ys(i,:) = y';

Error in Untitled (line 20) 
    [xsi1,ysi1] = Ieuler(f,[x0,xf],y0,0.1);

【问题讨论】:

    标签: matlab


    【解决方案1】:
    1. 停止在一个文本中写入多个逻辑行(以; 结尾) 线。甚至不清楚是哪个命令导致错误,因为 这一行有两个! (顺便说一句,这是ys(i+1,:) = y';
    2. 剥离您的代码。您应该提供一个最小的可重现示例 (它不是艺术比赛......并且由于Ieuler 中出现错误,其余部分没有必要)。
    3. 您的错误是:您的函数句柄“f”返回一个向量,但 y 是一个数组 => 您将得到一个矩阵,您想将其分配给数组 ys(i+1,:) = y'(无论出于何种原因,您都在转置这个)。 解决方案:f 返回一个数组:f=@(x,y) [y(2), (2/x)*y(2)-(2/x^2)*y(1)];(注意逗号)或转置其返回值s1 = f(x,y).';(将.' 用于非复杂转置是一种很好的做法——为清楚起见。

    建议:看看如何在 MATLAB 中进行调试。您可能可以通过在发生错误的行之前放置一个断点(甚至激活 Pause on Errors)并检查尺寸来跟踪这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多