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

1 ビュー (過去 30 日間)
Eva Carrillo 2019 年 11 月 19 日

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!

サインインしてコメントする。

### 回答 (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.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by