ans =
Solving for a variable in an equation
Mostrar comentarios más antiguos
Hello I have a matlab code below with an equation where I am trying to solve for N. However, everytime that I run the code it just runs forever and never gives me an answer. Any help would be great.
R = 0.1;
K = (7.516*10^3)*N^-0.6738 + 5.339;
M = (5.392*10^3)*N^-0.1606 + 190.847;
syms N
eqn = (1-R)*M*sqrt((((K/(1-R)*M)^2)*(1/pi))/(1000 + (((K/(1-R)*M)^2)*(1/pi)))) - 100 == 0;
S = solve(eqn)
This 1 equation comes from a system of 4 equations so if there is any other gbetter way to solve for it I am open to try different ways. Thanks
4 comentarios
Sean Marcus
el 27 de Mzo. de 2025
Sam Chak
el 27 de Mzo. de 2025
@Sean Marcus, I have two simple questions for you. From which textbook do all these nonlinear equations originate? Are there any examples of similar problems that we can refer to numerically solve for N?
Steven Lord
el 27 de Mzo. de 2025
If these equations represent something physically realizable (a machine that you're hoping to build and need N as some physical measurement of one of the components) can you state what the units of the various constants and variables ought to be? I wonder if this is a problem of dimensional analysis, where one of the quantities has units of meters but it was supposed to be included in the equation as a length in kilometers or vice versa.
Respuestas (2)
First, you need to recognize there is NO analytical solution to your problem. So using solve means it will just toss and turn forever.
Next, define N as a sym, BEFORE you use it in an equation!
syms N
R = 0.1;
K = (7.516*10^3)*N^-0.6738 + 5.339;
M = (5.392*10^3)*N^-0.1606 + 190.847;
eqn = (1-R)*M*sqrt((((K/(1-R)*M)^2)*(1/pi))/(1000 + (((K/(1-R)*M)^2)*(1/pi)))) - 100;
Remove the ==0, as that is implicit. But this allows us to plot it. I'll make it a function handle for better speed.
fun = matlabFunction(eqn);
fplot(fun,[0,1e15])
Hmm. I had to go out as far as 1e15, and it does not seem to be getting near zero.
fplot(fun,[0,1e30])
It looks like we can go on forever, and never reach zero. Do you see it seems to be slowing down, and will never get there?
While there may be complex roots, or negative roots, my guess is they would not be useful to you.
This means you may have made a mistake in generating this problem. We cannot know where the mstake lies. But there is no solution to be found.
6 comentarios
Sean Marcus
el 27 de Mzo. de 2025
syms N
R = 0.1;
K = (7.516*10^3)*N^-0.6738 + 5.339;
M = (5.392*10^3)*N^-0.1606 + 190.847;
eqn = (1-R)*M*sqrt((((K/(1-R)*M)^2)*(1/pi))/(1000 + (((K/(1-R)*M)^2)*(1/pi)))) - 100;
limit(eqn, N, inf)
vpa(ans)
Never reaches zero.
John D'Errico
el 27 de Mzo. de 2025
Editada: John D'Errico
el 27 de Mzo. de 2025
A log scale is completely irrelevant. Your equation NEVER crosses zero.
Is there a better way? No. You cannot find a point where that crosses the x axis, so at y==0.
What you need to do is to go back to your problem. Have you made a mistake? Surely, if you think a solution should exist. You may be using the wrong equation. Maybe you formulated the mathematics incorrectly. But you have reached a point where an impossibility exists. There is no positive real solution to the equation you have formulated.
syms N
R = 0.1;
K = (sym(7516)/sym(10)^3*sym(10)^3)*N^-(sym(06738)/sym(10)^4) + sym(5339)/sym(10)^3;
M = (sym(5392)/sym(10)^3*sym(10)^3)*N^-(sym(01606)/sym(10)^4) + sym(190847)/sym(10)^3;
eqn = (1-R)*M*sqrt((((K/(1-R)*M)^2)*(1/sym(pi)))/(sym(1000) + (((K/(1-R)*M)^2)*(1/sym(pi))))) - sym(100);
limit(eqn, N, inf)
simplify(ans)
vpa(ans)
Sean Marcus
el 28 de Mzo. de 2025
dpb
el 28 de Mzo. de 2025
I believe also @Steven Lord's comment on dimensional analysis is quite a solid point to ensure -- the big numbers in there at least raise the uncertainty levels. And, again, if there were any background provided to go with the bare equations it could be enlightening as to where there could be an issue.
function F=eqn(N)
R= 0.1;
K= (7.516*10^3)*N.^-0.6738 + 5.339;
M= (5.392*10^3)*N.^-0.1606 + 190.847;
F=(1-R).*M.*sqrt((((K./(1-R).*M).^2).*(1/pi))./(1000 + (((K./(1-R).*M).^2).*(1/pi)))) - 100;
end
fn=@eqn;
N0=1;
%N=fsolve(fn,N0)
fplot(fn,[1000 1E20])
N=logspace(1,20);
subplot(2,1,1)
K=(7.516*10^3)*N.^-0.6738 + 5.339;
K(end)
loglog(N,K)
legend('K')
subplot(2,1,2)
M=(5.392*10^3)*N.^-0.1606 + 190.847;
M(end)
loglog(N,M)
legend('M')
F=eqn(1E20)
The M, K terms approach asymptotic values as N gets large at roughly the final constant term. Evaluating your function at that point shows it's still quite a long way from 0 and will never get there...
figure
F=eqn(N);
F(end)
semilogx(N,F)
ylim([0 1000])
2 comentarios
Sean Marcus
el 27 de Mzo. de 2025
The fsolve formulation above works as template; of course it couldn't find a solution so I switched over to showing why. Recast that approach for a numerical solution instead of symbolic. Whether there will be a real solution will depend upon what the two unknown A and W are. K and M approach the constant term asymptotically as shown so you can make guesses about what the ranges could be for sanity checks. Is there any reason to think of a particular value/range for N?
It's possible if could explain what these represent somebody might recognize the problem...
Categorías
Más información sobre Calculus en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





