Iterative Process to find a variable in which two equations are equal and opposite
11 views (last 30 days)
Show older comments
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
0 Comments
Answers (3)
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)
sqrt(Eo/Uo)*cot(Wc*sqrt(Uo*Eo)*(b-h))
sqrt(Ed/Ud)*cot(Wc*sqrt(Ud/Ed)*h)
0 Comments
Les Beckham
on 11 Jul 2023
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
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
0 Comments
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)
There will of course be infinitely many other solutions. Remember the importance of plotting the curve BEFORE you just throw a solver at it.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!