can i use more than 1 function in same m. file ? because i want to use multiple function for adapting frequency
이전 댓글 표시
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
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);
Aniket
2013년 2월 10일
채택된 답변
추가 답변 (1개)
Walter Roberson
2013년 2월 10일
0 개 추천
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
2013년 2월 10일
Walter Roberson
2013년 2월 10일
What error did you encounter when you tried to put multiple functions in the same file?
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!