10 views (last 30 days)

Show older comments

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?

Abhishek Gupta
on 17 Feb 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

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

Start Hunting!