Pass a structure to a function

25 vues (au cours des 30 derniers jours)
George Bashkatov
George Bashkatov le 28 Fév 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);

Réponse acceptée

Walter Roberson
Walter Roberson le 3 Avr 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 commentaires
Walter Roberson
Walter Roberson le 3 Avr 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 le 4 Avr 2021
thank you a lot

Connectez-vous pour commenter.

Plus de réponses (1)

Robert U
Robert U le 28 Fév 2021
Modifié(e) : Robert U le 4 Mar 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
  1 commentaire
Walter Roberson
Walter Roberson le 3 Avr 2021
that doesn't solve my problem

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by