Duffing Oscillator issue. "Keep getting Unable to perform assignment because the left and right sides have a different number of elements."
1 次查看(过去 30 天)
显示 更早的评论
The Duffing Oscillator is as follows:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/248945/image.png)
To do this, we have to use the Euler-Cromer method to solve, which follows the following syntax:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/248946/image.png)
and
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/248947/image.png)
My code is as follows:
clc;
T=10;
N=1000;
deltat=T/N;
omega=1.2;
alpha=1.5;
beta=0;
delta=0;
gamma=0;
x=[N 1];
y=[N 1];
t=[N 1];
x(1)=0;
y(1)=0;
x2=[N 1];
y2=[N 1];
x2(1)=x(1);
y2(1)=y(1);
t(1)=0;
for n=1:N
x(n+1)=x(n)+y(n)*deltat;
y(n+1)=y(n)+deltat*(gamma*cos(omega*t)-delta*x(n)-alpha*x(n+1)-beta*x(n+1).^3);
x2(n+1)=x2(n)+y2(n)*deltat;
y2(n+1)=y2(n)-k*y2(n)*deltat-(omega^2)*cos(x2(n+1))*deltat;
t(n+1)=t(n)+deltat;
end
figure(1)
plot(t,x);
hold on;
plot(x,y);
I just keep getting "Unable to perform assignment because the left and right sides have a
different number of elements." For the line which I went ahead and bolded
I've never used the Euler-Cromer method before, so maybe I'm doing something wrong, but honestly am not sure. Help would be much appreciated!
0 个评论
回答(1 个)
James Tursa
2019-11-19
t is a vector, so your cos(omega*t) is a vector. You need to use cos(omega*t(n)) there.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!