Iterative Process to find a variable in which two equations are equal and opposite

11 views (last 30 days)
I am struggling to write a code that will find the multiple values of a variable in two equations where the equations will be equal in magnitude but opposite in sign. There can be a miniscule tolerance.
Here is what I have so far, it's definitely not functional, but I'm not sure what to do here.
% Equation
Eo=1;
Uo=1;
Fc=1.03;
Wc=2*pi*Fc;
b=0.3;
Ed=100;
Ud=1;
h = input('Enter initial value h: ');
A = sqrt(Eo/Uo)*cot(Wc*sqrt(Uo*Eo)*(b-h));
B = sqrt(Ed/Ud)*cot(Wc*sqrt(Ud/Ed)*h);
Error = A-B;
iter = 1;
while Error > 1e-3 && iter < 1000 %To avoid infinite loops
h = h+0.0001
A = sqrt(Eo/Uo)*cot(Wc*sqrt(Uo*Eo)*(b-h));
B = sqrt(Ed/Ud)*cot(Wc*sqrt(Ud/Ed)*h);
Error = A-B;
iter = iter + 1;
end
if iter == 1000
warning('Max Iterations reached')
end

Answers (3)

Torsten
Torsten on 11 Jul 2023
Edited: Torsten on 11 Jul 2023
Here is at least one solution for h:
% Equation
Eo=1;
Uo=1;
Fc=1.03;
Wc=2*pi*Fc;
b=0.3;
Ed=100;
Ud=1;
h0 = 10;
fun = @(h)sqrt(Eo/Uo)*cot(Wc*sqrt(Uo*Eo)*(b-h))+sqrt(Ed/Ud)*cot(Wc*sqrt(Ud/Ed)*h);
h = fsolve(fun,h0)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
h = 10.9966
sqrt(Eo/Uo)*cot(Wc*sqrt(Uo*Eo)*(b-h))
ans = -9.0826
sqrt(Ed/Ud)*cot(Wc*sqrt(Ud/Ed)*h)
ans = 9.0826

Les Beckham
Les Beckham on 11 Jul 2023
I would use fminsearch for this. See below.
Note that I am using abs(A - B) for the Error term.
% minimize error between two equations by adjusting the h parameter
minimizeError % call the main minimization function
Iteration Func-count f(x) Procedure 0 1 154.02 1 2 146.616 initial simplex 2 4 133.725 expand 3 6 113.617 expand 4 8 86.965 expand 5 10 56.7086 expand 6 12 37.4199 expand 7 14 16.459 expand 8 16 9.35441 expand 9 18 2.62892 reflect 10 20 2.04807 contract outside 11 22 0.522464 contract inside 12 25 0.522464 shrink 13 27 0.522464 contract outside 14 29 0.189874 contract inside 15 31 0.131747 contract inside 16 33 0.0361019 contract inside 17 35 0.0361019 contract inside 18 37 0.00443706 contract inside 19 39 0.00443706 contract inside 20 41 0.00443706 contract inside 21 43 0.000679668 contract inside 22 45 0.000679668 contract inside 23 47 0.000598173 contract inside Optimization terminated: the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-04 and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-04 Error is minimized with h = 2.161133
function minimizeError()
% Equation
Eo=1;
Uo=1;
Fc=1.03;
Wc=2*pi*Fc;
b=0.3;
Ed=100;
Ud=1;
h = 0.1; %input('Enter initial value h: ');
opt = optimset('Display', 'iter');
h = fminsearch(@costfun, h, opt);
fprintf('\nError is minimized with h = %f', h);
function Error = costfun(h) % define the cost function for the minimization
A = sqrt(Eo/Uo)*cot(Wc*sqrt(Uo*Eo)*(b-h));
B = sqrt(Ed/Ud)*cot(Wc*sqrt(Ud/Ed)*h);
Error = abs(A - B);
end
end

John D'Errico
John D'Errico on 11 Jul 2023
Edited: John D'Errico on 11 Jul 2023
h is the unknown. The problem becomes simple, but even so, there are some serious issues you need to understand.
Eo=1;
Uo=1;
Fc=1.03;
Wc=2*pi*Fc;
b=0.3;
Ed=100;
Ud=1;
A_h = @(h) sqrt(Eo/Uo)*cot(Wc*sqrt(Uo*Eo)*(b-h));
B_h = @(h) sqrt(Ed/Ud)*cot(Wc*sqrt(Ud/Ed)*h);
You want to solve for h, such that the two are the same value. However, you STATE that you are looking for a solution where the two have opposite signs. I think you really meant that the difference is zero, so the two are EXACTLY equal, not opposite in sign. That conclusion is based on your search for a zero of A-B.
First, we wil plot the difference.
A_B = @(h) A_h(h) - B_h(h);
fplot(A_B)
yline(0,'k')
And here we should see there will be infinitely many solutions. i assume you want the principal solution, so the first positive one?
fplot(A_B,[0,1])
yline(0,'k')
ylim([-100,100])
grid on
It looks like there is a zero crossing near 0.3, but the slope of the curve there will be extremely high, and that solution happens within a hair's breath of a singularity. That means I cannot easily give fzero a bracket. A good starting value, based on the information available here form the plot will be sufficient.
[hsol,fval,exitflag] = fzero(A_B,.29)
hsol = 0.2970
fval = 3.0695e-12
exitflag = 1
There will of course be infinitely many other solutions. Remember the importance of plotting the curve BEFORE you just throw a solver at it.

Categories

Find more on Mathematics 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!