How to get rid of the Error in Transcendental equation ?
조회 수: 1 (최근 30일)
이전 댓글 표시
Can anyone help to solve this problem . The error I am getting is : "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.Error in (line 17) :NEFF1(ii)=double(vpasolve(Eqn));" My code is given below:
clc
clearvars
close all
nf=1.84;
ns=1.48;
nc=1.3;
rho=1;
lambda=950:10:1850;
hf = 120;
m= 0;
syms neff
NEFF = zeros(size(lambda));
for ii=1:numel(lambda)
phi_c = -atan((nf/nc)^(2*rho)*sqrt ((neff^2-nc^2)/(nf^2-neff^2)));
phi_s = -atan((nf/ns)^(2*rho)*sqrt ((neff^2-ns^2)/(nf^2-neff^2)));
Eqn = (((2*pi./lambda(ii))*(sqrt(nf^2-neff^2)*hf))+phi_c+ phi_s -(m*pi))==0;% equation 8 from paper
NEFF1(ii)=double(vpasolve(Eqn));
NEFF(ii)= abs(NEFF1(ii));
deltaz_c(ii) = (lambda(ii)./2*pi).*(NEFF(ii)^2-nc^2).^(-0.5).*((NEFF(ii)./nf).^2+(NEFF(ii)./(nc.^2)-1)).^(-rho);%equation 6 from paper
deltaz_s(ii) = (lambda(ii)./2*pi).*(NEFF(ii)^2-ns^2).^(-0.5).*((NEFF(ii)./nf).^2+(NEFF(ii)./(ns.^2)-1)).^(-rho);
heff(ii) = deltaz_c( ii)+ deltaz_s(ii) + hf; %equation 5 from paper
P_c(ii)= ((nf.^2-NEFF(ii).^2)./(nf^2-nc^2)).*(deltaz_c(ii)./heff(ii)); %equation 4 from paper
S(ii)= (NEFF(ii)./nc).* P_c(ii).*((2*(NEFF(ii)./nc).^2)-1).^rho;%equation 7 from paper
end
plot(lambda, NEFF)
hold on;
plot(lambda , S)
Help me to solve this
댓글 수: 1
Jan
2021년 11월 5일
Note: This is not twitter, so we do not use # before the tags. All questions concern "matlab", so I've removed this hint.
채택된 답변
Star Strider
2021년 11월 5일
The problem is that ‘Eqn’ has no solution. The symbolic result is an empty vector, and that is throwing the error. The fsolve function just gives up aftar a while and returns the minimum, since ‘Eqn’ has no apparent root. (If ‘Eqn’ had a solution, it would have two values since it is a function of and the single indexing would throw the same error, although for a different reason.)
nf=1.84;
ns=1.48;
nc=1.3;
rho=1;
lambda=950:10:1850;
hf = 120;
m= 0;
syms neff
NEFF = zeros(size(lambda));
for ii=1:numel(lambda)
phi_c = -atan((nf/nc)^(2*rho)*sqrt ((neff^2-nc^2)/(nf^2-neff^2)));
phi_s = -atan((nf/ns)^(2*rho)*sqrt ((neff^2-ns^2)/(nf^2-neff^2)));
Eqn = (((2*pi./lambda(ii))*(sqrt(nf^2-neff^2)*hf))+phi_c+ phi_s -(m*pi)) % equation 8 from paper
NEFF1_Numeric = fsolve(matlabFunction(Eqn),rand*10)
NEFF1(ii)=double(vpasolve(Eqn))
NEFF{ii}= abs(NEFF1{ii})
deltaz_c{ii} = (lambda(ii)./2*pi).*(NEFF(ii)^2-nc^2).^(-0.5).*((NEFF(ii)./nf).^2+(NEFF(ii)./(nc.^2)-1)).^(-rho);%equation 6 from paper
deltaz_s{ii} = (lambda(ii)./2*pi).*(NEFF(ii)^2-ns^2).^(-0.5).*((NEFF(ii)./nf).^2+(NEFF(ii)./(ns.^2)-1)).^(-rho);
heff{ii} = deltaz_c( ii)+ deltaz_s(ii) + hf; %equation 5 from paper
P_c{ii}= ((nf.^2-NEFF(ii).^2)./(nf^2-nc^2)).*(deltaz_c(ii)./heff(ii)); %equation 4 from paper
S{ii}= (NEFF(ii)./nc).* P_c(ii).*((2*(NEFF(ii)./nc).^2)-1).^rho;%equation 7 from paper
end
plot(lambda, NEFF)
hold on;
plot(lambda , S)
.
댓글 수: 0
추가 답변 (1개)
Jan
2021년 11월 5일
Use the debugger to find out, what's going on:
dbstop if error
Matlab will stop in this line:
NEFF1(ii)=double(vpasolve(Eqn));
Now check the output of the right side:
double(vpasolve(Eqn))
It is empty. There is no solution for the equation. Thefore you cannot assign it to the scalar NEFF1(ii).
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!