Having a problem troubleshooting code (relatively simple code)

조회 수: 2 (최근 30일)
Vidhan Malik
Vidhan Malik 2016년 3월 20일
댓글: Star Strider 2016년 3월 23일
Running a relatively simple code to plot 2 variables against each other.
clear;clc;
syms A1 B2 SWR
R=0.5;
Cp=0.5;
Cv=0.3;
R = Cv^2*((1/Cv-tan(A1))^2 - (tan(B2))^2)/2/(1-Cv*tan(A1) - Cv*tan(B2)); %%Eq 1
Cp = 1 - (tan(B2)/(1/Cv - tan(A1)))^2; %%Eq2
i = 1;
while Cv<101
i = i + 1;
Cv= Cv + 0.01;
[A1,B2] = vpasolve([R, Cp], [A1,B2]); %%Solving Eq1 and Eq2 simultaneously for A1 and B2
SWR = 1 - Cv*(tan(A1) + tan(B2)); %%using A1 and B2 to solve for SWR
X(i,2) = Cv;
Y(i,2) = SWR;
end
plot(X(:,2),Y(:,2),'g-');
hold on
My end goal here is to plot Cv on the X axis and SWR on the Y axis. But for some reason, it seems to be entering into some sort of infinite loop somewhere since its taking way too long to give an output or even an error. I have tried debugging it but I still am not able to find where the problem is occurring.

채택된 답변

Star Strider
Star Strider 2016년 3월 20일
The Symbolic Math Toolbox is not the best way to solve iterative problems.
I coded your equations as anonymous functions and gave them to the Optimization Toolbox fsolve function that works best for problems like these. It works, but the ‘SWR’ value is negative (and since I believe that stands for ‘standing wave ratio’ on a transmission line, absolutely does not make sense, at least in that context).
Look over your equations to be sure they are set up correctly:
Cp=0.5;
% Cv=0.3;
Cvv = [0.3:0.1:101];
R = @(A1,B2,Cv) Cv.^2.*((1./Cv-tan(A1)).^2 - (tan(B2)).^2)./2./(1-Cv.*tan(A1) - Cv.*tan(B2)); %%Eq 1
Cp = @(A1,B2,Cv) 1 - (tan(B2)./(1./Cv - tan(A1))).^2; %%Eq2
% MAPPING: A1 = p(1), B2 = p(2)
Eqn = @(p,Cv) [R(p(1),p(2),Cv), Cp(p(1),p(2),Cv)];
for k1 = 1:length(Cvv)
Cv = Cvv(k1);
[P(:,k1),Fval] = fsolve(@(p)Eqn(p,Cv), [-145; 23]);
SWR(k1) = 1 - Cv*(tan(P(1,k1)) + tan(P(2,k1)));
end
figure(1)
plot(Cvv, SWR)
grid
xlabel('C_v')
ylabel('SWR')
I did the symbolic solution first with the initial value of ‘Cv’ (0.3), and used that solution ‘[-145,23]’ for the initial estimates in my loop. If you believe they should be other values, change them, run the loop, and see what works best. Since your equations are periodic, the ‘correct’ initial parameter estimates are important.
  댓글 수: 19
Vidhan Malik
Vidhan Malik 2016년 3월 23일
Ah doing it that way is leading to a whole host of errors haha. So now I am attempting to do it the anonymous function method via the following:
syms B1 B2 SWR Cv n
Cv=0.39;
R = @(Cv) 0.5 == Cv/2*(tan(B1*pi/180) + tan(B2*pi/180));
Cp = @(Cv) 0.5 == 1- (1+tan(B2*pi/180)^2)/(1+tan(B1*pi/180)^2);
i = 0;
while i<60
i = i + 1;
Cv= Cv+ 0.01;
[B1,B2] = fsolve([R(Cv), Cp(Cv)], [B1,B2]);
SWR = Cv*(tan(B1*pi/180) - tan(B2*pi/180));
X(i,2) = Cv;
Y(i,2) = SWR;
end
This unfortunately gives me the error of:
Error using fsolve (line
148)
FSOLVE requires the
following inputs to be of
data type double: 'X0'.
Error in s (line 16)
[B1,B2] =
fsolve([R(Cv), Cp(Cv)],
[B1,B2]);
Any ideas why?
Star Strider
Star Strider 2016년 3월 23일
Yes. I use the new equations with my previous fsolve code to get this:
Cvv = [0.3:0.1:101];
R = @(B1,B2,Cv) -0.5 + Cv/2*(tand(B1) + tand(B2)); %%Eq 1
Cp = @(B1,B2,Cv) -0.5 + (1+tand(B2).^2)/(1+tand(B1).^2); %%Eq2
% MAPPING: A1 = p(1), B2 = p(2)
Eqn = @(p,Cv) [R(p(1),p(2),Cv), Cp(p(1),p(2),Cv)];
for k1 = 1:length(Cvv)
Cv = Cvv(k1);
[P(:,k1),Fval] = fsolve(@(p)Eqn(p,Cv), [1; 1]);
SWR(k1) = Cv*(tand(P(1,k1)) - tand(P(2,k1)));
end
figure(1)
plot(Cvv, SWR)
grid
xlabel('C_v')
ylabel('SWR')
Note that I substituted tand (tangent with degree arguments) for the radian-argument conversion in the code required for the Symbolic Math Toolbox, since you’re using degree arguments. The degree-argument functions are more accurate, especially for small angles.
I use [1; 1] as the initial (degree) estimates for the angles. Change them if that is not correct, since I’ve no idea what they should be.
It takes about 23 seconds to run on my machine.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by