我试图绘制一个积分L,相对于Xdot,但我一直得到矢量必须是相同的长度错误,但我不知道如何解决它。我的代码如下所示。您可以在底部看到,总和的循环最多只能达到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')
答案 0 :(得分:4)
在绘图时,如何缩短x
一个?
plot (m(1:end-1),L, 'r')