can i use more than 1 function in same m. file ? because i want to use multiple function for adapting frequency

1 次查看(过去 30 天)
Hello
i want to simulate using multiple oscillators.
following figure is showing what i want to do ..
the following code is having only one oscillator. I want to simulate using 2 or more oscillators. anyone is having idea how to do simulate with multiple oscillator ?
///////////////////////////////////////////
adaptive hopf oscillator function (instead of 1 i want to use 2 same function)
///////////////////////////////////////////
function dz = myeqd(t,y,ti,xd)
dz = zeros(3,1);
mu=0.2;
r= sqrt(y(1)^2 + y(2)^2);
K=100;
F=interp1(ti,xd,t);
dz(1)= (mu - r^2)*y(1) - y(3)*y(2) +K*F;
dz(2) = (mu - r^2)*y(2) + y(3)*y(1);
dz(3) = (-K*F) * (y(2)/sqrt(y(1)^2 + y(2)^2));
///////////////////////////////////////////////////
main code
///////////////////////////////////////////////////
Tspan= 0:0.001:20; % time vector
f0=100;
f1=400;
fi = chirp(Tspan,f0,20,f1,'quadratic');
ti=Tspan;
[T,Y]=ode45(@(t,y) myeqd1(t,y,ti,fi),Tspan,[1;1;90]);
plot (T,Y(:,3));
  2 个评论
ChristianW
ChristianW 2013-2-10
编辑:ChristianW 2013-2-11
Just curious, why not use 1 ODE y' = f(t,y) with all states in statevector y?
% Osci#1
mu = 0.2; K = 100;
r = sqrt(y(1)^2 + y(2)^2);
dz(1) = (mu - r^2)*y(1) - y(3)*y(2) +K*F;
dz(2) = (mu - r^2)*y(2) + y(3)*y(1);
dz(3) = (-K*F) * (y(2)/r);
% Osci#2
mu2 = 0.2; K2 = 100;
r2 = sqrt(y(4)^2 + y(5)^2);
dz(4) = (mu2 - r2^2)*y(4) - y(6)*y(5) +K2*F;
dz(5) = (mu2 - r2^2)*y(5) + y(6)*y(4);
dz(6) = (-K2*F) * (y(5)/r2);

请先登录,再进行评论。

采纳的回答

ChristianW
ChristianW 2013-2-11
  1. The attempt having myeqd1, myeqd2, myeqd3, ... and then solve them with ode45(myeqd1,...); ode45(myeqd2,...); ... etc is bad in general.
  2. At your figure the oscillators are connected. If they influence each other, you need all oscillators in one ODE.
  3. Whats the difference between Osci#1 and #2? Only mu and K?
  4. My example above only shows that possibility. If you try it, you should still use "multiple functions":
function dy = myeqd(t,y,ti,xd)
dy = zeros(size(y));
F = interp1(ti,xd,t);
dy(1:3) = Osci(y(1:3),F,0.2,100); % Osci#1, Osci(y,F,mu,K)
dy(4:6) = Osci(y(4:6),F,0.1,120); % Osci#2
function dz = Osci(z,F,mu,K)
r = sqrt(z(1)^2 + z(2)^2);
dz = [ (mu - r^2)*z(1) - z(3)*z(2) + K*F
(mu - r^2)*z(2) + z(3)*z(1)
-K*F * z(2)/r ];
  3 个评论
Aniket
Aniket 2013-2-11
hello Christian,
I have add feedback but results are becoming zeros i don't why ? you have any idea ? i am plotting output by using summation "sum_out = Y(:,3)+Y(:,6)"
if i am adding feedback , i am getting following curve ,its not adapting
code
***************
function
****************
function dz = myeqd(t,y,ti,xx)
dz = zeros(6,1);
mu = 0.3;
r= sqrt(y(1)^2 + y(2)^2);
K1=500;
K2 = 500;
F=interp1(ti,xx,t)-(y(3)+y(6));
%osc1
dz(1)= (mu - r^2)*y(1) - y(3)*y(2) +K1.*F;
dz(2) = (mu - r^2)*y(2) + y(3)*y(1);
dz(3) = (-K1.*F) * (y(2)/sqrt(y(1)^2 + y(2)^2));
%osc2
dz(4)= (mu - r^2)*y(4) - y(6)*y(5) +K2.*F;
dz(5) = (mu- r^2)*y(5) + y(6)*y(4);
dz(6) = (-K2.*F) * (y(5)/sqrt(y(4)^2 + y(5)^2));
**************************************
main code
***************************
time = 0:0.001:20;
xx = chirp(time,100,10,400,'linear');
ti=time;
[T,Y]=ode45(@(t,y) myeqd(t,y,ti,xx),time,[5;5;90;5;5;90]);
sum_out = Y(:,3)+Y(:,6);
plot (T,sum_out)

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2013-2-10
Yes, it is quite valid to have more than one function in a .m file.
Only the first of the functions (the one the file is named with) will be directly callable from outside the .m file; other .m files would need to somehow have been given a function handle before they could call the other functions.
In your situation where you are passing in an anonymous function, that is sufficient for ode45 to get the function handle to be able to call the function referred to in the anonymous function.
  2 个评论
Aniket
Aniket 2013-2-10
I have tried more than one function in single m.file but it gives me error. Ok, I want to create a group of oscillator function like in the above figure mentioned , so that external input frequency i can adapt and i can call these group of funtion from my main m.file. how should i crate it in matlab ?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by