【问题标题】:Vectors must be the same length error向量必须是相同的长度错误
【发布时间】:2017-11-23 10:45:54
【问题描述】:

我正在尝试绘制一个关于 Xdot 的积分 L,但我不断收到向量必须是相同长度的错误,但我不知道如何解决它。我的代码如下所示。您可以在底部看到,求和的循环仅上升到 99,而 x_1 上升到 100。但是,如果我更改循环,则会出现错误“索引超出矩阵尺寸”

    % The solution for this part is based on the Euler method

    f=0;       %initializing the force row vector
    f_1=0.5;   %initializing the first derivative of force
    x=1;       % intializing the mass displacement%
    x_1(1)=0;       % initializing the first derivative of mass displacement
    t=0;       % initializing the time row vector
    j=1;         % initializing a`enter code here`n index used in iterations
    a=0;
    b=10;
    N=100;
    h=(b-a)/N;


for j = 0:N-1

f_2=-1*sin(f);     %obtain the second derivative of the force 

f_1=f_1+f_2*h;      %obtain the first derivative of the force

f=f+f_1*h; % obtain the value of force and concatenate it with 
                     %preceding force row vector the element

x_2=f-0.1*x_1-x-x^3;  %obtain the second derivative of the mass displacement

x_1=x_1+x_2*h;                % obtain the first derivative of the mass displacement

x=x+x_1*h; % obtain the  current value of mass displacement and 
                     %concatenate it with preceding mass displacement row vector the element

t=t+h;  % obtain the current value of time and concatenate it with 
                     %preceding time row vector the element     
j=j+1;  %increment the index iterator by one

v(j)=x;
w(j)=t;
m(j)=x_1
end

sum = 0; %%Trapezoidal method to find L, sum is L, put this at the end of your script 
for i = 1:size(m,2)-1
   sum = sum + h*(m(i+1)^2+m(i)^2)/2;
   L(i) = sum
end

plot (m,L, 'r') 

【问题讨论】:

    标签: matlab


    【解决方案1】:

    在绘图时将x 缩短一个怎么样?

    plot (m(1:end-1),L, 'r')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多