How to create matrix from a function
29 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Gabriella Gonnella
el 14 de Feb. de 2021
Comentada: Gabriella Gonnella
el 25 de Feb. de 2021
Hi guys, I need to create a matrix (in my script is "array"), deriving from a function, depending on three variables, which alternate.
clear all
Var; %from which I derive T1 and m1
beta= 0.05;
k0 = 1.67*10^13;
R = 0.0083;
for j = 1:length(T1)
alpha1_tga(j) = (m1(1,1)-m1(j))./(m1(1,1)-m1(end,end));
alpha1_tga(j) = 1 - alpha1_tga(j);
end
E1= 150;
E2 = 300;
Estep = 5;
sigma1 = 10;
sigma2 = 70;
sigmastep = 5;
[alpha_daem,h2,array] = solvedaem(T1,beta,k0,R,E1,Estep,E2,sigma1,sigmastep,sigma2,alpha1_tga);
Here, there's the function.
function [alpha_daem,h2,array] = solvedaem(T1,beta,k0,R,E1,Estep,E2,sigma1,sigmastep,sigma2,alpha1_tga)
for E0 = E1:Estep:E2
for sigma = sigma1:sigmastep:sigma2
h2 = 0;
array = [];
for j = 1:length(T1)
f = @(x) (exp(-k0./beta.*R.*T1(j).^2./x.*exp(-x./R./T1(j))))./(sigma.*sqrt(2*pi)).*exp(-(x-E0).^2./(2.*sigma.^2));
a = E0-25; b = E0+25; %lower and upper limits
nSimp = 100; %number of intervals
hSimp = (b - a)/nSimp; %length of each interval
s = 0;
for i=0:nSimp
if i==0 || i==nSimp
p = 1;
elseif mod(i,2) ~= 0
p = 4;
else
p = 2;
end
x = a + i*hSimp;
s = s + p*f(x);
end
alpha_daem(j) = hSimp/3*s;
h2 = h2+(alpha1_tga(j) - alpha_daem(j)).^2;
end
array = [E0,sigma,h2];
end
end
end
I mean that I need to create the matrix "array", in which the first column in equal to E1 ( from 150 to 300 with step 5), the second column equal to sigma (from 10 to 70 with step 5, that should repeat for each value of E1), and third column equal to h2.
Could anyone help me?
0 comentarios
Respuesta aceptada
Abhishek Gupta
el 17 de Feb. de 2021
Hi,
As per my understanding, you want to create a matrix containing E1, sigma, and h2 values. The simple way would be as follows:-
Approach 1 - size of the output matrix is known
output = zeros(size(E1:Estep:E2,2)*size(sigma1:sigmastep:sigma2,2), 3); % initialize the matrix
count = 1;
for E0 = E1:Estep:E2
for sigma = sigma1:sigmastep:sigma2
%-------%
end
output(count, :) = [E0,sigma,h2]; % append the array
count = count + 1;
end
Approach 2 - size of the output matrix is unknown
output = []; % initialize the matrix
for E0 = E1:Estep:E2
for sigma = sigma1:sigmastep:sigma2
%-------%
end
output = [output; [E0,sigma,h2]]; % append the array
end
3 comentarios
Abhishek Gupta
el 19 de Feb. de 2021
Editada: Abhishek Gupta
el 19 de Feb. de 2021
As far as I understand, you want a 30x2 matrix (column 1 - E, column 2 - sigma), where you want to repeat sigma (13x1) to fill up the 30x1 column. You can do the same as follow: -
sigma = rand(13,1); % replace this with your sigma
len = 30; % length of the matrix
extendedSigma = [repmat(X, floor(len / numel(X)), 1); ...
X(1:mod(len, numel(X)))];
output = [E sigma]; % desired matrix -- Col 1 - E, Col 2 - sigma
For more information, check out the link below: -
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!