MATLAB Answers

Duffing Oscillator issue. "Keep getting Unable to perform assignment because the left and right sides have a different number of elements."

2 views (last 30 days)
Eva Carrillo
Eva Carrillo on 19 Nov 2019
Answered: James Tursa on 19 Nov 2019
The Duffing Oscillator is as follows:
To do this, we have to use the Euler-Cromer method to solve, which follows the following syntax:
and
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!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!