Problem in solving a differential equations system
Show older comments
Hi, I'm trying to solve non linear propagation equations for second harmonic generation (lasers)
Here is my code
function ypoint = EquaDiff( y,z )
c=3e8;
eps0=8.85e-12;
lw=1.030;
w=2*pi()*c/(lw*1e-6);
deff=0.828e-12;
n= 1.605;
q=w*deff/(n*c);
deltak=0;
ypoint=zeros(2,1);
ypoint(1)=j*q*conj(y(1))*y(2);
ypoint(2)=j*q*(y(1)^2);
ypoint=ypoint';
clear all;
[y,z]=ode45 (@EquaDiff, [0:0.001:0.03],[1 0]);
y1=y(:,1);
y2=y(:,2);
plot (z,abs(y1).^2,z,abs(y2).^2);
I get the following error
?? Attempted to access y(2); index out of bounds because numel(y)=1.
Error in ==> EquaDiff at 16
ypoint(1)=j*q*conj(y(1))*y(2);
Error in ==> funfun\private\odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> f at 4
[y,z]=ode45 (@EquaDiff, [0:0.001:0.03],[1 0]);
Does somebody has an idea what i did wrong?
Answers (3)
Walter Roberson
on 24 Nov 2011
0 votes
The supplied function (e.g., EquaDiff) is expected to have pass a scalar t as its first argument, and a column vector y as its second argument. Therefore accessing element #2 of the first argument is an error.
Your function EquaDiff only uses one of its two arguments. Is your system independent of time ?
DEYRA
on 25 Nov 2011
0 votes
Hello, I'm with a similar error: This is my code:
function J = f(Q, h)
A = 1.54e-2; %area of both tanks
c12 = 6e-4; %Flow constant of the interconnectiong pipe
c2 = 13e-4;
y1 = (1/A)*(Q(1) - c12*sqrt(h(1) - h(2)));
y2 = (1/A)*(Q(2) + c12*sqrt(h(1) - h(2)) - c2*sqrt(h(2)));
J = [y1; y2];
end
Nt = 101; %Step Size of time
ti = 0; %Initial time (sec)
tf = 10; %Final time (sec)
t = linspace(ti,tf,Nt); %Time vector (sec)
h = [1.7 0.3]; %Initial liquid levels
Q= [1 1]; %Initial pump flow
% Q1=1;
% Q2=1;
%Initial value for ode45 routine
initial1 = [Q h];
[t1,y1] = ode45(@f,t,[Q, h]);
And I get this error: Attempted to access Q(2); index out of bounds because numel(Q)=1.
1 Comment
I guess you mean
Nt = 101; %Step Size of time
ti = 0; %Initial time (sec)
tf = 10; %Final time (sec)
tspan = linspace(ti,tf,Nt); %Time vector (sec)
h = [1.7 0.3]; %Initial liquid levels
Q= [1 1]; %Initial pump flow
[t1,y1] = ode45(@(t,y)f(t,y,h),tspan,Q);
function J = f(t, Q, h)
A = 1.54e-2; %area of both tanks
c12 = 6e-4; %Flow constant of the interconnectiong pipe
c2 = 13e-4;
y1 = (1/A)*(Q(1) - c12*sqrt(h(1) - h(2)));
y2 = (1/A)*(Q(2) + c12*sqrt(h(1) - h(2)) - c2*sqrt(h(2)));
J = [y1; y2];
end
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!