Pass a structure to a function

22 visualizaciones (últimos 30 días)
George Bashkatov
George Bashkatov el 28 de Feb. de 2021
Comentada: George Bashkatov el 4 de Abr. de 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);

Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Abr. de 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 comentarios
Walter Roberson
Walter Roberson el 3 de Abr. de 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 el 4 de Abr. de 2021
thank you a lot

Iniciar sesión para comentar.

Más respuestas (1)

Robert U
Robert U el 28 de Feb. de 2021
Editada: Robert U el 4 de Mzo. de 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

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by