Why are the inputs to the functions used in the ODE45 solver not in the right syntax?

1 view (last 30 days)
My code should solve an ODE that contains functions which are stored in seperate files. My problem is taht i dont know how to prperly give the inputs to the functions so it is transferred over several files. I was told i should try giving the additional inputs to the function when i call it rather than making the variables global (having the variables global worked the other option did not work yet). Should i keep it global or is the other approach also fine with the right syntax? And what is wrong about my code, that it doesnt work when trying to give the inputs to the function?
Thanks in advance.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This is in thé main script:
field = [1,0,1 ; 1,1,1];
%in my script i loop through each element of the field-matrix and excute code afterwards but to make it easier to %understand i left that out since its not important for the question
idx1 = 1;
idx2 = 2;
startA = 10;
A = 10;
currbugs = 12;
tspan = [0,1];
y0 = [startA];
[t,y]=ode45(@Aph,tspan,y0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%This is the function which is in a seperate file:
function sol=Aph(t,y,field,idx1,idx2,currbugs)
rgr = 10;
A = y(1);
P = (PRL1(field,idx1,idx2,A)*currbugs(2,1))+(PRad(field,idx1,idx2,A)*currbugs(7,1));
sol = rgr*A*(1-(A/2500))-P;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Within the calculation of the ODE45 equation other function PRL1 and PRad are called but they %are also in seperate files:
function out = PRL1(field,idx1,idx2,A)
sr = 39.1;
ht = 10;
if field(idx1,idx2) == 1
SAI = 1;
else
SAI = 0.6;
end
out = (sr*(A/SAI))/(1+sr*ht*(A/SAI));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%and the second function:
function out = PRad(field,idx1,idx2,A)
sr = 0.0139;
ht = 0.0049
if field(idx1,idx2) == 1
SAI = 1;
else
SAI = 0.6;
end
out = sr*(A/SAI)/(1+sr*ht*(A/SAI));

Accepted Answer

Torsten
Torsten on 22 Nov 2022
Edited: Torsten on 23 Nov 2022
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This is in thé main script:
field = [1,0,1 ; 1,1,1];
%in my script i loop through each element of the field-matrix and excute code afterwards but to make it easier to %understand i left that out since its not important for the question
idx1 = 1;
idx2 = 2;
startA = 10;
A = 10;
currbugs = [1;1;1;1;1;1;7];
tspan = [0,1];
y0 = [startA];
[t,y]=ode45(@(t,y)Aph(t,y,field,idx1,idx2,currbugs),tspan,y0);
plot(t,y)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%This is the function which is in a seperate file:
function sol=Aph(t,y,field,idx1,idx2,currbugs)
rgr = 10;
A = y(1);
P = (PRL1(field,idx1,idx2,A)*currbugs(2,1))+(PRad(field,idx1,idx2,A)*currbugs(7,1));
sol = rgr*A*(1-(A/2500))-P;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Within the calculation of the ODE45 equation other function PRL1 and PRad are called but they %are also in seperate files:
function out = PRL1(field,idx1,idx2,A)
sr = 39.1;
ht = 10;
if field(idx1,idx2) == 1
SAI = 1;
else
SAI = 0.6;
end
out = (sr*(A/SAI))/(1+sr*ht*(A/SAI));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%and the second function:
function out = PRad(field,idx1,idx2,A)
sr = 0.0139;
ht = 0.0049;
if field(idx1,idx2) == 1
SAI = 1;
else
SAI = 0.6;
end
out = sr*(A/SAI)/(1+sr*ht*(A/SAI));
end
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!