Pass a structure to a function

32 Ansichten (letzte 30 Tage)
George Bashkatov
George Bashkatov am 28 Feb. 2021
I'm trying to pass this structure to the function, but matlab writes: Dot indexing is not supported for variables of this type. Error in famplifire (line 3). How I can fix that?
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
global Par;
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
dy1 = y(1).*(-Par.sigma_pa.*Par.N0+(Par.sigma_pa+Par.sigma_pe).*(k.*y(1)./(y(1)+y(2)+1)));
dy2 = y(2).*Par.sigma_se.*(k.*y(1)./(y(1)+y(2)+1));
dydz = [dy1; dy2];
And a part of main code:
global Par;
Par=struct;
Par.sigma_pa=0.6*10^(-18)*10^(-4);
Par.N0=1*10^19*10^6;
Par.sigma_pe=0.3*10^(-18)*10^(-4);
Par.sigma_se=0.9*10^(-18)*10^(-4);
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
________________________________________________________________
zspan=[0 l];
startval=[b; a];
[z1,y1]=ode45(@(z,y) famplifire(Par,k,y,z),zspan,startval);

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Apr. 2021
zspan=[0 l];
startval=[b; a];
You did not define l or a or b in what you posted.
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
global Par;
The Par that is passed in is being replaced by the global par. In a release soon, it will simply be an error to use global with the same variable name as a parameter.
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
You pass in k, but you immediately overwrite it.
I did not encounter the problem you indicate, but like @Robert U indicated, that problem could be caused by using global.
  2 Kommentare
Walter Roberson
Walter Roberson am 3 Apr. 2021
Par=struct;
Par.sigma_pa=0.6*10^(-18)*10^(-4);
Par.N0=1*10^19*10^6;
Par.sigma_pe=0.3*10^(-18)*10^(-4);
Par.sigma_se=0.9*10^(-18)*10^(-4);
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
l = 1e-2;
b = 1;
a = 2;
zspan=[0 l];
startval=[b; a];
[z1,y1]=ode45(@(z,y) famplifire(Par,k,y,z),zspan,startval);
plot(z1, y1)
function dydz = famplifire(Par,k,y,~) %Function with parameters to be passed
dy1 = y(1).*(-Par.sigma_pa.*Par.N0+(Par.sigma_pa+Par.sigma_pe).*(k.*y(1)./(y(1)+y(2)+1)));
dy2 = y(2).*Par.sigma_se.*(k.*y(1)./(y(1)+y(2)+1));
dydz = [dy1; dy2];
end
George Bashkatov
George Bashkatov am 4 Apr. 2021
thank you a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Robert U
Robert U am 28 Feb. 2021
Bearbeitet: Robert U am 4 Mär. 2021
Hi George Bashkatov,
First of all: Do not use global variables if not absolutely necessary. There are hundreds of threads arguing global variable troubles that are easily avoided by not using global variables.
Most propably your global variable assignment mixed something up. Neither could I reproduce the error nor could I execute your code directly. Please, make sure that all variables are supplied when posting code.
My suggestion is to perform a input test of your function inputs:
function dydz = famplifire(Par,k,y,z) %Function with parameters to be passed
validateattributes(Par,{'struct'},{'nonempty'});
cFieldnames = fieldnames(Par);
cNeededFieldnames = {'N0', 'sigma_pa', 'sigma_pe'};
if ~all(ismember(cNeededFieldnames,cFieldnames))
error('Variable ''Par'' does not supply all needed fields.')
end
k=Par.N0*Par.sigma_pa/(Par.sigma_pa+Par.sigma_pe);
dy1 = y(1).*(-Par.sigma_pa.*Par.N0+(Par.sigma_pa+Par.sigma_pe).*(k.*y(1)./(y(1)+y(2)+1)));
dy2 = y(2).*Par.sigma_se.*(k.*y(1)./(y(1)+y(2)+1));
dydz = [dy1; dy2];
end
Kind regards,
Robert

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by