how can I save the data generated from a function that depends on 3 parameters

Hello,
I have a function that depends on 3 parameters. I want to create a database by varying each parameter and for this, I will use three for cycles:
For Wpig=3:0.5:8
For chla=0.6:0.2:0.5
For starch_content=2:4:52
[Wavelength,Ea,Es,b,C_abs,C_sca] = proprad(Wpig,chla,starch_content)
Each evaluated condition generates 6 results (Wavelength, Ea, Es, b_C_abs,C_sca). I want to stock these 6 values for each condition, Do you think I should create a mesh/grid for it? It is important for me to be able to use the database created at the end to interpolate.
Could someone help me? I I will be very grateful for your suggestions and advice. Thank you a lot in advance.

 Réponse acceptée

Assuming the outputs of proprad are all scalar:
[Wpig, chla, starch_content] = ndgrid(3:0.5:8, 0.6:0.2:0.5, 2:4:52);
[Wavelength, Ea, Es, b, C_abs, C_sca] = arrayfun(@proprad, Wpig, chla, starch_content);
If the outputs are not scalar, then add 'UniformOutput', false to the arrayfun call.
That's also assuming that the proprad function cannot operate directly on array inputs. Otherwise, the arrayfun is not necessary.

12 commentaires

In fact , I forgot to mention that the output are vectors. Does the solution you propose to me work with? If you answer is yes, Can I after create the database interpolate wihtout problem?
thank you a lot!!!!
I'm a bit unclear on what an interpolation between two vectors would be. For example, if at point (1, 1, 1) you have the vector [1, 2, 3] and at point (2,2,2) you have the vector [5, -6]. What is the result of the interpolation at point (1.5, 1.1, 1.9) ?
I will try to be more clear.... With the generated database I want to known what's the value of my 6 outputs (being vectors) for one condition between thoses calculated with my function previously, for exemple: from the database interpolate to know what is the 6 output for Wpig =4.3, chla=0.55 and starch_content=37.5.
thanks again!
Yes, I understand what you want to interpolate between. What's not clear is what interpolation means for a vector.
If at Wpig = 4, chla = 0.5, starch_content = 34, wavelength is the vector [1, 2, 3] and
if at Wpig = 4.5, chla = 0.7, starch_content = 38, wavelength is the vector [5, -6]
what is wavelength at Wpig =4.3, chla=0.55, starch_content=37.5?
in fact, for the 3 cycles for, I'm going to generate 6 vector corresponding for: wavelength, Ea, Es, b, C_abs, C_sca. Each conditions generate with your proposed code these 6 vectos. So, when I'm going to interpolate, I want to have the interpolation for the 6 vectors. Each vector has around 50 values.
Guillaume
Guillaume le 3 Oct 2019
Modifié(e) : Guillaume le 3 Oct 2019
I don't undestand what you are saying. For a scalar Wpig, chla and starch_content is each output of proprad scalar (i.e. is wavelength scalar, etc?).
For a scalar Wpig, chla and starch_content is each output of proprad a vector...
Hi Guillaume, I hope you can see this comment. I reformulated my coding problem, waiting you can better undestand what I need:
I have 3 vectors conditions:
first vector: Wpig=0.5:0.5:7;
second vector: Chla=0.5:0.01:0.6;
third vector: Starch=0:0.04:0.24;
Wavelength:linspace(300,700,31)
for each possible condition I calculate Ea using a function I named "proprad(Wpig,Chla,Starch)", so for exemple for proprad(0.5,0.5,0)=Ea and Ea is a vector composed by 31 values (it's for this I created a vector named Wavelength having the same lenght that Ea vector result). Summarizing, usign my function, when you put 3 scalars: Wpig,Chla,Starch, you get a vector result (Ea).
So, I want to create a 4D matrix in wich the 3 first dimensions will correspond to my 3 conditions (Wpig,Chla,Starch) and the 4th dimension will be the vectors estimated by my function.
My first idea is to create a zeros 4D matrix like this:
Mat_Ea=zeros(length(Wpig),length(Chla),length(Starch),length(Wavelength));
and then remplace each dimension by the value, giving something like this:
1 dimension : Wpig
2 dimension: Chla
3 dimension: Starch
4 dimension: Ea
finally, my goal is to use the 4D matrix, for interpolation. So for exemple, if I want to know what's the value of Ea for conditions like Wpig=0.93,Chla=0.57 and Starch=0.12, I will be able to interpolate in the 4D matrix to estimate it.
Could you help me to solve this coding problem?
Thank you very much for your time and your advise!
That's a lot clearer indeed. Two questions:
  • Isn't Wavelength an input of proprad as well? Or is a constant within the function?
  • Does proprad work only with scalar inputs. Couldn't you pass matrices of equal size for Wpig, Chla, and Starch?
  • Wavelength is not an input of "proprad" , I put it because it has the same dimension of Ea and it could be used for the 4D-matrix construction but I'm not sure. So, you can erase/forget this vector.
  • "proprad" evaluates only scalar inputs, so we need to use "for" cycles in order to evaluate all the conditions.
First of all, I think I need to create a grid for all the possible conditions, giving something like this:
[X,Y,Z] = ndgrid(Wpig,Chla,Starch);
I would need help for the next parts: generate the 4D-Matrix, knowing that for each point (X,Y,Z) I obtain a vector (Ea) from my function "proprad".
If necessary, Wpig, Chla, Starch can have the same dimension. I must respect only the bounds of these vectors.
Thank you a lot one more time!
You can use arrayfun and ndgrid as I initially answered:
Wpig = 0.5:0.5:7;
Chla = 0.5:0.01:0.6;
Starch = 0:0.04:0.24;
[Wpig3D, Chla3D, Starch3D] = ndgrid(Wpig, Chla, Starch); %creates 3D matrices
reshapefun = @(w, c, s) reshape(proprad(w, c, s), 1, 1, 1, []); %function to move the vector output of proprad into the 4th dimension
Ea = cell2mat(arrayfun(reshapefun, Wpig3D, Chla3D, Starch3D, 'UniformOutput', false)); %create 3D cell arrays of vectors and convert to matrix
If proprad can directly return a 1x1x1xN vector, then you don't need the reshapefun and can directly do:
Ea = cell2mat(arrayfun(@proprad, Wpig3D, Chla3D, Starch3D, 'UniformOutput', false)); %create 3D cell arrays of vectors and convert to matrix
which will run faster.
Or you can use a loop indeed, which may be faster:
Wpig = 0.5:0.5:7;
Chla = 0.5:0.01:0.6;
Starch = 0:0.04:0.24;
Ea = zeros(numel(Wpig), numel(Chla), numel(starch), 31);
for sidx = 1:numel(Starch)
for cidx = 1:numel(Chla)
for widx = 1:numel(Wpig)
Ea(widx, cidx, sidx, :) = proprad(Wpig(widx), Chla(cidx), Starch(sidx));
end
end
end
Either way, once you've got the 4D matrix, it's easy to interpolate, ndgrid your 4 dimensions (Wpig, Chla, Starch and Wavelength), then build your interpolant with griddedInterpolant and query it at whichever points you want:
Wpig = 0.5:0.5:7;
Chla = 0.5:0.01:0.6;
Starch = 0:0.04:0.24;
Wavelength = linspace(300,700,31);
[Wpig4d, Chla4D, Starch4D, Wavelength4D] = ndgrid(Wpig, Chla, Starch, Wavelength);
%build interpolant
Finterp = griddedInterpolant(Wpgid4D, Chla4D, Starch4D, Wavelength4D, Ea);
%query interpolant for wpig = .93, Chla = 0.57, Starch = 0.12 at all wavelengths:
result = Finterp([repmat([.93, .57, .12], numel(Wavelength), 1), Wavelength(:)])
Great!!! That's working perfect!
Thank you a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by