MATLAB: How to change the parameters and then evaluate the function again

evaluate the function again

format long;
t=1:100:8460000;
k=7e-6;b=13.8; F=zeros(size(t));
c=3e8;
v=0.064.*c;
eNi=3.9e10;
eCo=6.8e9;
tNi=8.8*86400;
tCo=111.3*86400;
M=1.9891e33;
Mej=6.5*M;
kr=[inf 0.014 inf 0.021 0.021 0.021 0.021 0.021];
A=4.75e13.*kr.*2.1*(0.62*3)^(-2);
M_Ni=[2.0 2.0 0 0 0.1 0.2 0.5 1.0]*M;
tm=sqrt((2.*k.*Mej)/(b.*v.*c));
for r=1:numel(t)
f=Pfunction(tm,eNi,eCo,tNi,tCo,M_Ni);
F(r)=integral(f,0,t(r));
end
L=(2/tm).*exp((-t.^2)./tm^2).*(1-exp(-A.*t.^2)).*F;
function f=Pfunction(tm,eNi,eCo,tNi,tCo,M_Ni)
f=@(x)exp(x.^2./tm^2).*x./tm.*eNi.* M_Ni.*exp(-x./tNi) + eCo.* M_Ni.*( ...
(exp(-x./tCo)-exp(-x./tNi))./(1-tNi/tCo));
This is what I have tried,but it showed that "The matrix dimensions must be consistent."I wanna change the parameters and then evaluate the function again,cause the parameter M_Ni is a matrix.What should I do ?

Best Answer

First, you do not need the ’Pfunction’ function, since it simply returns ‘f’ as a function. Just use ’f’ directly.
After that, the loop becomes simply:
f=@(x)exp(x.^2./tm^2).*x./tm.*eNi.* M_Ni.*exp(-x./tNi) + eCo.* M_Ni.*( ...
(exp(-x./tCo)-exp(-x./tNi))./(1-tNi/tCo));
F=zeros(numel(t),numel(M_Ni));
for r=1:numel(t)
F(r,:)=integral(f,0,t(r), 'ArrayValued',1);
end
This leaves the problem with ‘L’ because ‘t’ is (1x84600) and ‘A’ is (1x8) and when I ran your code, ‘A(1)’ and ‘A(3)’ were Inf.
Related Question