Function for ODE45 requires all coefficients be written in it

Hi all,
I wanted to create a function for ODE45 operation. This is how it looks like:
zfunction FSM_solutions = FSM(t,f)
a = 2; g_p = 5; g_s = 105; g_a = 0; g = 1; k = 250;
eta = 2; P = 0; O_x = 0; O_y = 0; O_z = 0; eta_substract = P * eta;
eta_add = eta;
% f(1)=E_pr; f(2)=E_mr; f(3)=E_pi; f(4)=E_mi; f(5)=N;
% f(6)=m_x; f(7)=m_y; f(8)=m_z; I_p=f(1).^2+f(3).^2;
% I_m=f(2).^2+f(4).^2;
FSM_solutions = [k*(f(5)+f(8)-1)*(f(1)-a*f(3))-g_a*f(1)+g_p*f(4)-O_z*f(3);
k*(f(5)-f(8)-1)*(f(2)-a*f(4))-g_a*f(2)+g_p*f(3)+O_z*f(4);
k*(f(5)+f(8)-1)*(f(3)+a*f(1))-g_a*f(3)-g_p*f(2)+O_z*f(1);
k*(f(5)-f(8)-1)*(f(4)+a*f(2))-g_a*f(4)-g_p*f(1)-O_z*f(2);
g*(eta_add-(1+f(1).^2+f(3).^2+f(2).^2+f(4).^2)*f(5)-(f(1).^2+f(3).^2-f(2).^2-f(4).^2)*f(8));
-(g_s+g*(f(1).^2+f(3).^2+f(2).^2+f(4).^2))*f(6)+O_y*f(8)-O_z*f(7);
-(g_s+g*(f(1).^2+f(3).^2+f(2).^2+f(4).^2))*f(7)+O_z*f(6)-O_x*f(8);
g*(eta_substract)-(g_s+g*(f(1).^2+f(3).^2+f(2).^2+f(4).^2))*f(8)-g*(f(1).^2+f(3).^2-f(2).^2-f(4).^2)*f(5)+O_x*f(7)-O_y*f(6)];
enz
As you might notice, the function requires all coefficients to be written in it. If I delete any of coeff, the function and ODE45 do not work. But I want to be able to change coefficients in a script file. Could you please suggest how I can do so?

Answers (2)

See http://www.mathworks.com/help/matlab/math/parameterizing-functions.html on how to create anonymous functions that include the required information

3 Comments

Thank you for your help!
However, it did not work this way. When I tried to make this function as annonymous, then try to write a coeff in the script file, I see the next error:
Error in FSM (line 10)
FSM_solutions = [k*(f(5)+f(8)-1)*(f(1)-a*f(3))-g_a*f(1)+g_p*f(4)-O_z*f(3);
it does not want to take my coeff O_z from the script file. Maybe you can write as it should be?
parameter1 = ...;
parameter2 = ...;
...
[T,F] = ode45(@(t,f) fun(t,f,parameter1,parameter2,....),tspan,y0)
function FSM_solutions = FSM(t,f,parameter1,parameter2,...)
...
end
Thank you so much for your help!
Unfortunetaly, it still does not work when I try to implement your solution. There is always a mistake in operations. May I share with you th function file and the script file, maybe it will be much more clear for you what I do wrong.

Sign in to comment.

tspan = linspace(0,35,35000);
f0 = [1; 1; 1; 1; 1; 1; 1; 1];
a = 2; g_p = 3; g_s = 105; g_a = 0; g = 1; k = 250; P = 0; O_z = 5;
eta = 2; O_x = 0; O_y = 0;
[t,f] = ode45(@(t,f)FSM(t,f,a,g_p,g_s,g_a,g,k,P,O_z,eta,O_x,O_y),tspan,f0);
E_pr=f(1:end,1); E_mr=f(1:end,2);
E_pi=f(1:end,3); E_mi=f(1:end,4); N=f(1:end,5);
m_x=f(1:end,6); m_y=f(1:end,7); m_z=f(1:end,8);
dE_pr = k * (N + m_z - 1).* (E_pr - a * E_pi) - g_a * E_pr + g_p * E_mi - O_z * E_pi;
dE_mr = k * (N - m_z - 1).* (E_mr - a * E_mi) - g_a * E_mr + g_p * E_pi + O_z * E_mi;
dE_pi = k * (N + m_z - 1).* (E_pi + a * E_pr) - g_a * E_pi - g_p * E_mr + O_z * E_pr;
dE_mi = k * (N - m_z - 1).* (E_mi + a * E_mr) - g_a * E_mi - g_p * E_pr - O_z * E_mr;
E_p = sqrt(E_pr.^2 + E_pi.^2);
E_m = sqrt(E_mr.^2 + E_mi.^2);
I_p = E_p.^2;
I_m = E_m.^2;
I = (I_p + I_m)./2;
E_x = (E_p + E_m)./sqrt(2);
E_y = (E_m - E_p)./sqrt(2);
I_x = E_x.^2;
I_y = E_y.^2;
I_difference = (I_p - I_m);
e = I_difference./(I_p + I_m);
figure(1)
plot(t(28000:32000),I_p(28000:32000),'-',t(28000:32000),I_m(28000:32000),'--');
axis([28 32 0 1.2])
xlabel('Time (ns)');
ylabel('CP Intensity (%)');
figure(2)
plot(t(28000:32000),e(28000:32000))
axis([28 32 -1 1])
xlabel('Time (ns)');
ylabel('Elipticity (%)');
figure(3)
plot(t(28000:32000),I_x(28000:32000),t(28000:32000),I_y(28000:32000))
axis([28 32 0 1.2])
xlabel('Time (ns)');
ylabel('LP Intensity (%)');
function FSM_solutions = FSM(t,f,a,g_p,g_s,g_a,g,k,P,O_z,eta,O_x,O_y)
eta_substract = P * eta;
eta_add = eta;
% f(1)=E_pr; f(2)=E_mr; f(3)=E_pi; f(4)=E_mi; f(5)=N;
% f(6)=m_x; f(7)=m_y; f(8)=m_z; I_p=f(1).^2+f(3).^2;
% I_m=f(2).^2+f(4).^2;
FSM_solutions = [k*(f(5)+f(8)-1)*(f(1)-a*f(3))-g_a*f(1)+g_p*f(4)-O_z*f(3);
k*(f(5)-f(8)-1)*(f(2)-a*f(4))-g_a*f(2)+g_p*f(3)+O_z*f(4);
k*(f(5)+f(8)-1)*(f(3)+a*f(1))-g_a*f(3)-g_p*f(2)+O_z*f(1);
k*(f(5)-f(8)-1)*(f(4)+a*f(2))-g_a*f(4)-g_p*f(1)-O_z*f(2);
g*(eta_add-(1+f(1).^2+f(3).^2+f(2).^2+f(4).^2)*f(5)-(f(1).^2+f(3).^2-f(2).^2-f(4).^2)*f(8));
-(g_s+g*(f(1).^2+f(3).^2+f(2).^2+f(4).^2))*f(6)+O_y*f(8)-O_z*f(7);
-(g_s+g*(f(1).^2+f(3).^2+f(2).^2+f(4).^2))*f(7)+O_z*f(6)-O_x*f(8);
g*(eta_substract)-(g_s+g*(f(1).^2+f(3).^2+f(2).^2+f(4).^2))*f(8)-g*(f(1).^2+f(3).^2-f(2).^2-f(4).^2)*f(5)+O_x*f(7)-O_y*f(6)];
end

1 Comment

Omg! it was wrong due to a unnecessary space between functions :) thank you so much!

Sign in to comment.

Products

Release

R2022b

Asked:

on 3 Nov 2022

Commented:

on 5 Nov 2022

Community Treasure Hunt

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

Start Hunting!