Hi Matlab Users,
I cannot understand what is wrong with my code. I would appreciate if anyone could help me.
I got this error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
% Define model parameters: d, Lambda, DeltaE, V, Om, kappa
clear; clc;
DeltaE = 0;
Lambda = 1e-1;
d = 12;
kappa = 1;
Ksi = 5e-5; % damping parameter
Om = 1e-2; % driving frequency
%V = 9.2e-3; % driving amplitude
V = 9e-4;
tspan=[0:1e5];
% Define the function with integration variables
fun_ode = @(t, y) shapiro(t,y,d,DeltaE,Lambda,V,Om,kappa);
% fun_shapiro is a function which contains th coupled differential equations of the driven undamped soliton-surface plasmon Josephson junction with asymmetric coupling. You need to code this.
% Integrator options
options = odeset('RelTol',1e-7,'AbsTol',[1e-8 1e-8]);
%Define time array in integer multiples of 2*pi/Om
t = 2*pi/Om*(1:200);
%Define a set of initial points Phi0,Z0. A coarse example is the following:
[Phi0,Z0] = meshgrid(-1:0.4:1,-1:0.4:1);
% initialize the phase space figure
figure(1),clf;
axis([-1.5 1 -0.3 0.3]);
grid on;
hold on;
% Integrate the system from each initial point and plot the results
[T, Y]=ode45(fun_ode,tspan,[Phi0(i,j) Z0(i,j)],options);
% Collapse the Phi values into the [-1,1] range.
Y(:,1) = wrapToPi(Y(:,1)*pi)./pi;
plot(Y(:,1),Y(:,2),'.k','MarkerSize',3);
% plot the same values displaced by 2*pi. This is just for a better looking figure:
hold on;
plot(Y(:,1)-2,Y(:,2),'.k','MarkerSize',3);
and this is shapiro function
function dydt = shapiro(t,y,DeltaE,Lambda,d,kappa,Ksi,Om,V)
dydt = zeros(2,1);
qc = fun_q(Lambda,d,y(2));
a = sqrt((1-y(2))./(1+y(2)));
dydt(1) = (DeltaE + Lambda.*y(2) - 0.5*(a.*kappa - 1./a)...
*qc.*cos(pi*y(1)) + V*cos(t))/(Om*pi);
dydt(2) = -.5*sqrt((1-y(2))*(1+y(2)))*qc*(1+kappa)...
*sin(pi*y(1))/Om - Ksi*pi*dydt(1);
end
in addition to that
function q = fun_q(Lambda,d,Z)
q = sqrt(.5*(1+Z))*exp(-d*sqrt(2*Lambda*(1+Z)));
%q = 1.d-5; % Constant q for BEC-Josephson model
end
With my regards,

4 件のコメント

madhan ravi
madhan ravi 2019 年 1 月 31 日
編集済み: madhan ravi 2019 年 1 月 31 日
upload ode functions (fun_ode)
fartash2020
fartash2020 2019 年 1 月 31 日
I get this error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Untitled2 (line 33)
[T, Y]=ode45(fun_ode,tspan,[Phi0(i,j) Z0(i,j)],options);
fartash2020
fartash2020 2019 年 1 月 31 日
I would highly appreciate if you could you give the link to that?
madhan ravi
madhan ravi 2019 年 1 月 31 日
respond to James answer below

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

 採用された回答

Star Strider
Star Strider 2019 年 1 月 31 日

0 投票

Could the problem be in or related to ‘fun_q’ that you have not posted?
It would likely help if you posted it.

6 件のコメント

fartash2020
fartash2020 2019 年 1 月 31 日
Sorry I missed that. I just edit the question
Walter Roberson
Walter Roberson 2019 年 1 月 31 日
fun_q is at the bottom of the Question
Star Strider
Star Strider 2019 年 1 月 31 日
@Walter — It is now. It wasn’t when I posted my Comment.
@fartash2020 — When I run your code, and define ‘i’ and ’j’ both being 1, I get this:
Not enough input arguments.
with respect to ‘shapiro’.
The reason is obvious. You call your ‘shapiro’ function with:
fun_ode = @(t, y) shapiro(t,y,d,DeltaE,Lambda,V,Om,kappa);
however you define your ‘shapiro’ function as:
function dydt = shapiro(t,y,DeltaE,Lambda,d,kappa,Ksi,Om,V)
You are calling it with fewer arguments (8, rather than the 9 required),and also with the the arguments out of order. In MATLAB, they must be passed in the order the function expects them. If you want to use the default value for an argument (not an issue here since you have not defined default values in ‘shapiro’), you would pass an empty argument [ ] in that position.
I must defer to you to solve this.
fartash2020
fartash2020 2019 年 1 月 31 日
編集済み: fartash2020 2019 年 1 月 31 日
Dear Star Strider,
I do appreciate your great comments. I have fixed Shapiro to Shapiro2:
function dydt = shapiro2(t,y,d,DeltaE,Lambda,V,Om,kappa,Ksi)
also in the main code:
fun_ode = @(t, y) shapiro2(t,y,d,DeltaE,Lambda,V,Om,kappa,Ksi);
I also added two for loops:
for i=1:6
for j=1:6
[T, Y]=ode45(fun_ode,tspan,[Phi0(i,j) Z0(i,j)],options);
end
end
This seems to work fine without error, but I do not get plot.
Thanks,
fartash2020
fartash2020 2019 年 1 月 31 日
I fixed the code and I got the plots, thank you all
Star Strider
Star Strider 2019 年 1 月 31 日
As always, my pleasure.

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

その他の回答 (1 件)

James Tursa
James Tursa 2019 年 1 月 31 日

0 投票

I don't see anywhere in your code where you define i and j before using them as indexes into Phi0 and Z0, so they default to the imaginary numbers, hence the error. Perhaps you intended to wrap all of this in for loops?

2 件のコメント

fartash2020
fartash2020 2019 年 1 月 31 日
yes your are right, but even if I choose any random i and j this does not work. lets set them 1 and 1 respectively. Maybe there is something wrong with the shapiro function that I have written,
James Tursa
James Tursa 2019 年 1 月 31 日
Are you saying that if you set i=1 and j=1 just prior to the line in question, that you get the exact same error? Or do you get a different error?

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2018b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by