How do I change the parameters and then 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 ?

 Accepted 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.

4 Comments

If I want to graph the function with different parameters,and A is o.o14,what should I do?
I do not understand. In the code you posted, ‘A’ is a (1x8) vector, with elements 4E+11 to 6E+11. It is straightforward to set it equal to 0.014. The 'ArrayValued' name-value pair will evaluate ‘F’ at each parameter value of ‘N_Mi’, so only the loop is needed to integrate it at every value of ‘t’.
The code then becomes:
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);
A = 0.014;
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));
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
figure
semilogy(t, F)
grid
xlabel('t')
ylabel('F')
lgdstrc = sprintfc('%.1E',M_Ni);
hl = legend(lgdstrc, 'Location', 'SE');
title(hl, 'M_{Ni}')
and the plot is:
How do I change the parameters and then evaluate the function again - 2019 05 18 & 2019 05 20.png
Experiment to get the result you want.
Your answer really help a lot,but I still have a question.Because kr and M_Ni both have eight values, what can I do to draw a graph for L with two 1x8 matrices?
L=(2/tm).*exp((-t.^2)./tm^2).*(1-exp(-A.*t.^2)).*F';
I have no idea what you are actually asking.
Since ‘F’ is a (84600 x 8) matrix, you could take the mean (or sum, or median or std or something else that produces one value from a vector) across dimension 1 of ‘F’. Any of those would result in a (1x8) vector.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!