Plotting the function of single variable
5 views (last 30 days)
Show older comments
I am trying to plot the function N_21(p,s,t) given below.
The code is the following
%--------------------------------------------------------------------------------------------------------------------------------------------
syms h k x p q J
n0 = 400;
r0 = 1/n0;
gamma = 1.26;
r=@(p) r0 * gamma.^p;
N_21 =@(p,s,t) r0 .* n0 .* (2 .* s ./ (2.*s - 1)).^(p+1) .* symprod( r(J), J,[0, p-1]).*...
(exp(- r0 .* (2.*s-1) .* t)./(symprod( r(J) - r0, J,[1,p]))+...
symsum( exp(- r(q) .* (2.*s - 1) .* t)./((r0-r(q)).* (symprod(r(h) - r(q), h, 0, q-1) .* ...
symprod(r(h) - r(q), h, q+1, p))), q,[1, p]));
p2=plot(t, N_21(1,0.85,t),'b--', 'LineWidth',1.5 );
%--------------------------------------------------------------------------------------------------------------------------------------------
"p" and "s" are the parameters and can take positive values (p=1,2,3,4,.... and 0<=s<1). The reason for putting p and s in the code is to be free in choosing any desired value to them.
I can't figure out the error in the code why this is not running. Any suggestion/solution, please.
2 Comments
John D'Errico
on 18 Aug 2021
What error do you get in the code? When you just tell us you got an error, there are many things you may have done wrong. This forces us to run your code, when the error message itself would tell us much about what happened. So when you want to ask about an error, show the COMPLETE text of the error message. EVERYTHING IN RED.
Answers (1)
David Hill
on 18 Aug 2021
n0 = 400;
r0 = 1/n0;
gamma = 1.26;
p=20;%assumed
s=.7;%assumed
P=1:(p+1);
r=r0*gamma.^(0:p);
N=@(t)r0*n0*(2*s/(2*s-1))^(p+1)*prod(r(1:end-1))*(exp(-r0*(2*s-1)*t)/prod(r(2:end)-r(1))+sum(exp(-r(2:end)*(2*s-1)*t)./(r(1)-r(2:end)).*arrayfun(@(x)prod(r(P~=x)-r(x)),2:(p+1))));
t=0:.1:10;
n=zeros(size(t));
for k=1:length(t)
n(k)=N(t(k));
end
plot(t,n);
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!