Unable to perform assignment because the left and right sides have a different number of elements.
조회 수: 4 (최근 30일)
이전 댓글 표시
I am trying to run this code but always get this error
r0=0.05;
k1=0.5;
k2=0.5;
mu=0.5;
rho=0.5;
epsilon=0.25;
K=1;
alpha=0.1;
q=0.1;
eta=0.05;
sigma=5;
zeta=0.05;
omega0=0.001;
NN1=5000;
NN2=5000;
TT=linspace(0.01,350,NN1);
syms M b Msol positive
Nut(M) = mu+(rho*M/(1+M)); % N(M)
gro(M) = r0*(1+k1*Nut(M)*(1-k2*Nut(M))); % r(M)
lam(M) = 1/(1+Nut(M)); % λ(Μ)
P(M) = epsilon*M/(1+M);
dNut(M) = diff(Nut(M),M); % Ν'(Μ)
dgro(M) = diff(gro(M),M); % r'(M)
dlam(M) = diff(lam(M),M); % λ'(M)
dP(M) = diff(P(M),M); % P'(M)
eqn = (q/b)*gro(M)*(eta+P(M))*(1+Nut(M))*(1-(((alpha*sigma*q*(eta+P(M))*(1+Nut(M))+b*(q+alpha)*(zeta*M-omega0)))/(b*alpha*sigma*K)))-(q/sigma)*(zeta*M-omega0)==0;
[num,den]=numden(lhs(eqn));
b_num = linspace(0.001,8,NN2);
for u = 1:numel(b_num)
Msol(u) = vpa(solve(subs(num,b,b_num(u))));
end
However when the vector b_num starts from 0.008, the code works. Is there a way I can include the values of b_num less than 0.008? Many thanks!
댓글 수: 0
채택된 답변
Torsten
2024년 2월 9일
r0=0.05;
k1=0.5;
k2=0.5;
mu=0.5;
rho=0.5;
epsilon=0.25;
K=1;
alpha=0.1;
q=0.1;
eta=0.05;
sigma=5;
zeta=0.05;
omega0=0.001;
NN1=5000;
NN2=5000;
TT=linspace(0.01,350,NN1);
syms M b Msol positive
Nut(M) = mu+(rho*M/(1+M)); % N(M)
gro(M) = r0*(1+k1*Nut(M)*(1-k2*Nut(M))); % r(M)
lam(M) = 1/(1+Nut(M)); % λ(Μ)
P(M) = epsilon*M/(1+M);
dNut(M) = diff(Nut(M),M); % Ν'(Μ)
dgro(M) = diff(gro(M),M); % r'(M)
dlam(M) = diff(lam(M),M); % λ'(M)
dP(M) = diff(P(M),M); % P'(M)
eqn = (q/b)*gro(M)*(eta+P(M))*(1+Nut(M))*(1-(((alpha*sigma*q*(eta+P(M))*(1+Nut(M))+b*(q+alpha)*(zeta*M-omega0)))/(b*alpha*sigma*K)))-(q/sigma)*(zeta*M-omega0)==0;
[num,den]=numden(lhs(eqn));
b_num = linspace(0.001,8,NN2);
Msol = nan(size(b_num));
for u = 1:numel(b_num)
sol = vpa(solve(subs(num,b,b_num(u))));
if ~isempty(sol)
Msol(u) = sol;
end
end
댓글 수: 3
Torsten
2024년 2월 9일
편집: Torsten
2024년 2월 9일
What is the old one ? I thought it did not work.
If you want a fast solution, you must use the numerical function "roots" to solve for the roots of your polynomial in M of degree 7 instead of the symbolic "solve".
If your equation has more than one positive solution for M given b, I took the first one.
r0=0.05;
k1=0.5;
k2=0.5;
mu=0.5;
rho=0.5;
epsilon=0.25;
K=1;
alpha=0.1;
q=0.1;
eta=0.05;
sigma=5;
zeta=0.05;
omega0=0.001;
NN1=5000;
NN2=5000;
TT=linspace(0.01,350,NN1);
syms M b
Nut(M) = mu+(rho*M/(1+M)); % N(M)
gro(M) = r0*(1+k1*Nut(M)*(1-k2*Nut(M))); % r(M)
lam(M) = 1/(1+Nut(M)); % λ(Μ)
P(M) = epsilon*M/(1+M);
dNut(M) = diff(Nut(M),M); % Ν'(Μ)
dgro(M) = diff(gro(M),M); % r'(M)
dlam(M) = diff(lam(M),M); % λ'(M)
dP(M) = diff(P(M),M); % P'(M)
eqn = (q/b)*gro(M)*(eta+P(M))*(1+Nut(M))*(1-(((alpha*sigma*q*(eta+P(M))*(1+Nut(M))+b*(q+alpha)*(zeta*M-omega0)))/(b*alpha*sigma*K)))-(q/sigma)*(zeta*M-omega0)==0;
[num,den]=numden(lhs(eqn));
p = fliplr(coeffs(num,M));
p = matlabFunction(p);
b_num = linspace(0.001,8,NN2);
Msol = nan(size(b_num));
for u = 1:numel(b_num)
sol = roots(p(b_num(u)));
sol = sol(real(sol)>0 & abs(imag(sol)) < 1e-8);
if ~isempty(sol)
Msol(u) = sol(1);
end
end
plot(b_num,Msol)
grid on
추가 답변 (1개)
Matt J
2024년 2월 9일
편집: Matt J
2024년 2월 9일
Your code assumes that solve() will always return one and only one solution. There is no reason to think that will always be the case (see below). So, the question becomes, what do you want to do when situations like in the examples below arise?
syms x real
vpa(solve(x^2==1))
vpa(solve(x^2==-1))
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!