Reputation: 1
I am new user with Matlab and I wrote this script. At the end I have to operate the formula written below. To the y(j,:)
value, it gives me the right answer but when I use for loop a second time the value of z(i,:)
gives me the wrong answer.I want element by element multiplication I checked answer by calculating it on calculator. Can someone look at this why I am getting wrong values for z(i,:)
% formula I(Q,t) = A exp(-t/tau_beta)^beta
% beta
beta= [0.4 0.6 0.8 1];
tau_beta = [1 10 100 1000];
t=[0:1:1e4];
% division of (t/tbeta)
for j= 1:1:4
y(j,:) = t/tau_beta(j);
for i = 1:1:4
z(i,:) = y(j,:).^beta(i);
A =1;
I_temp(i,:) = A*exp(-z(i,:));
I(i,j,:) = I_temp(i,:);
end
end
Upvotes: 0
Views: 91
Reputation: 973
It is much easier to evaluate functions over some parameter space by using the functions meshgrid
(for spaces consisting of 2 parameters) or ndgrid
(for spaces of any dimensionality). These basically are a convenient way of doing the required repmat
's to create the space to evaluate your function over.
Use like so:
[bm,tbm,tm] = ndgrid(beta,tau_beta,t);
I = A*exp(-tm./tbm).^bm;
ndgrid
creates matrices spanning the space of its input arguments, doing any calculations with these is like evaluating the function for all possible combinations of your input parameters.
Using bsxfun
you can eliminate the extra memory used by first computing the grid, but it gets rather illegible quickly.
Upvotes: 1