MATLAB: Using variables between m-files

passing variables

I want to use an input dialog box for inputs in one m-file and use these inputs in another function…
Here is the first code:
x = inputdlg({'Rate', 'Population renewal', 'Death rate'}, 'Inputs')
b = str2double(x{1});
p = str2double(x{2});
l = 0.5;
d = 0.9;
m = str2double(x{3});
v = (b*p)*(1+(l*d));
c = m*(m+l);
R = /c;
if (R<1)
answer = msgbox(['Value = ' num2str(R) ' therefore there is no epidemic'],'Basic Reproduction number')
else
answer = msgbox(['Value = ' num2str(R) ' therefore there is an epidemic'],'Basic Reproduction number')
end
---------------------------------------------------------------------------------------------
I want to use the same inputs in another function and I am using the following code:
function msir = msirtry(t,y)
load('salRo.m','l','p','b','m','d');
d = 0.9;
c = 0.14;
a = 0.25;
r = 0.05;
msir(1) = p - m*y(1)- b*y(1)*y(2)*(1+(l*d)) + a*y(4);
msir(2) = b*y(1)*y(2)*(1+(l*d)) - (m + l)*y(2);
msir(3) = l*d*y(2) - (m + r)*y(3);
msir(4) = l*c*y(2) + r*y(3) - (m + a)*y(4);
msir = msir(:);
--------------------------------------------
and i am running this function using
yo = [20 5 5 10];
[t y] = ode45(@msirtry,[0 5],yo);
plot(t,y(:,1),t,y(:,2),t,y(:,3),t,y(:,4),'Linewidth',2.5)
legend('Susceptible','Infected Symptomatic','Infected Asymptomatic','Recovered')
--------------------------------------------------------------
I get the error – Number of columns on line 1 of ASCII file
C:\Users\Krishnaa\Documents\MATLAB\salRo.m
must be the same as previous lines.
if i manually put in the inputs instead of loading it, the codes work but I need to work all this out together… pls help…

Best Answer

Aircode -- untested!!! :)
This uses the nested function solution technique so that the variables in the function misfunc which is called from the script in model.m are visible inside the function used as the handle to ode45. This technique is described more in the documentation under first nested functions for them specifically then for their use in such situations w/ links from ode45
model.m
x = inputdlg({'Rate', 'Population renewal', 'Death rate'}, 'Inputs')
b = str2double(x{1});
p = str2double(x{2});
l = 0.5;
d = 0.9;
m = str2double(x{3});
v = (b*p)*(1+(l*d));
c = m*(m+l);
R = /c; % problem here...this intended to be 1/c, maybe???
yo = [20 5 5 10];
[t y] = msifunc,b,p,m,l,d,yo);
plot(t,y(:,1),t,y(:,2),t,y(:,3),t,y(:,4),'Linewidth',2.5)
legend('Susceptible','Infected Symptomatic','Infected Asymptomatic','Recovered')
% This seems peculiar -- the answer isn't anywhere a function of the
% other function and integral??? This is dependent entirely on the input
% constants...oh, well, ...
if (R<1)
answer = ...
else
answer = ...
end
msifunc.m
function [t,y]=msifunc(b,p,m,l,d,yo)
[t y] = ode45(@msirtry,[0 5],yo);
function msir = msirtry(t,y)
msir=zeros(4,1);
d = 0.9;
c = 0.14;
a = 0.25;
r = 0.05;
msir(1) = p - m*y(1)- b*y(1)*y(2)*(1+(l*d)) + a*y(4);
msir(2) = b*y(1)*y(2)*(1+(l*d)) - (m + l)*y(2);
msir(3) = l*d*y(2) - (m + r)*y(3);
msir(4) = l*c*y(2) + r*y(3) - (m + a)*y(4);
end
end
Related Question