# Solving system of nonlinear equations using Matlab

87 visualizzazioni (ultimi 30 giorni)
Ahmed Khan il 7 Feb 2024
Modificato: DGM il 1 Apr 2024 alle 20:24
I am trying to solve a system of nonlinear equations using Matlab. I have been trying to us the fsolve() and Isqnonlin() to solve the function yet both return incorrect answers. Specifically, I am trying to solve the Steinmetz Equation. I have the Power, the magnetic field in [SPAM REMOVED], and the frequency and need , , and n. The equation is However, Matlab keeps giving me a value of less than 0 and n is less than 0. n should be from 1-4 and ke should be a positive number less than one.
%% Nonlinear equations
equations = @(x) [x(1)*24.57^2*1.6^2 + x(2)*24.57*1.6^x(3) - 0.4;
x(1)*50.63^2*1.3^2 + x(2)*50.63*1.3^x(3) - 1;
x(1)*100.4^2*1.1^2 + x(2)*100.4*1.1^x(3) - 1.9];
x0 = [0 0 0];
solution = fsolve(equations, x0)
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.
solution = 1×3
-0.0003 0.0675 -1.2506
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ke = solution(1)
ke = -3.3766e-04
kh = solution(2)
kh = 0.0675
n = solution(3)
n = -1.2506
%% Check results
f1 = ke*24.57^2*1.6^2 + kh*24.57*1.6^n - 0.4
f1 = -7.0943e-14
f2 = ke*50.63^2*1.3^2 + kh*50.63*1.3^n - 1
f2 = -1.1502e-13
f3 = ke*100.4^2*1.1^2 + kh*100.4*1.1^n - 1.9
f3 = -1.0081e-13
I tried fsolve and that I would get positive numbers. I understand that fsolve just looks for a solution and that there may be multiple solutions, but I can't figure how to fix this. I have tried the lsqnonlin and adding bounds to my problem, but I think matlab runs into an issue, because it makes n equal to the lower bound no matter what.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
John D'Errico il 1 Apr 2024 alle 18:36
Perhaps we should just close the question then, despite the answers.
DGM il 1 Apr 2024 alle 20:22
Modificato: DGM il 1 Apr 2024 alle 20:24
At least to me the thread seems helpful. I think the given answers are clear and insightful, but I'm playing the dumb janitor here. I'd defer to your opinion regarding the quality of the question and its potential value to others.
I haven't really looked at the question to discern whether it's plausibly someone's real question, or if someone just threw together some AI generated question-like code salad. I just find it hard to believe that a single-use spammer account would actually put human effort into a well formatted macguffin.

Accedi per commentare.

### Risposte (2)

Torsten il 7 Feb 2024
Modificato: Torsten il 7 Feb 2024
You equations only have a negative solution for x3 as you can see from the function plot.
syms x1 x2 x3
f1 = x1*24.57^2*1.6^2 + x2*24.57*1.6^x3 - 0.4 == 0;
f2 = x1*50.63^2*1.3^2 + x2*50.63*1.3^x3 - 1 == 0;
f3 = x1*100.4^2*1.1^2 + x2*100.4*1.1^x3 - 1.9 == 0;
x11 = solve(f1,x1);
f2 = subs(f2,x1,x11);
f3 = subs(f3,x1,x11);
x21 = solve(f2,x2);
x22 = solve(f3,x2);
f(x3) = x21-x22
f(x3) =
fplot(f,[-10 20])
limit(f,x3,Inf)
ans =
0
grid on
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

John D'Errico il 7 Feb 2024
Modificato: John D'Errico il 8 Feb 2024
fsolve does not apply constraints. It does not know that you think some coefficient should be some number. It does not care. All it cares about is where you start it, and what are the equations. Sometimes your equations are specified incorrectly. Sometimes you just gave it crappy starting values.
Yes, you can use lsqnonlin, which does allow bound constraints. But the fact that lsqnonlin strongly wants to push the solution to a "bad" place should tell you something.
equations = @(x) [x(1)*24.57^2*1.6^2 + x(2)*24.57*1.6^x(3) - 0.4;
x(1)*50.63^2*1.3^2 + x(2)*50.63*1.3^x(3) - 1;
x(1)*100.4^2*1.1^2 + x(2)*100.4*1.1^x(3) - 1.9];
x0 = [1 1 1];
format long g
solution = fsolve(equations, x0)
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.
solution = 1x3
-0.000337662743718997 0.0675332211134897 -1.25058458523988
equations(solution)
ans = 3x1
1.0e+00 * 0 2.22044604925031e-16 -4.44089209850063e-16
And that is clearly a solution, but not the one you like.
Can we investigate if a solution exits at all where you think there should be one? We can eliminate two of the variables easily enough.
syms ke kh n
eq = equations([ke,kh,n])
eq =
Eliminate ke from the problem. This is just like Gaussian elimination. Isolate ke in the first equation, then replace ke in the other two.
kesol = solve(eq(1),ke)
kesol =
eq2 = subs(eq(2:3),ke,kesol)
eq2 =
Next, eliminate kh from the problem.
khsol = [isolate(eq2(1) == 0,kh),isolate(eq2(2) == 0,kh)]
khsol =
Then just subtract the two.
neq = khsol(1) - khsol(2)
neq =
neq is a function ONLY of n. If a solution exists for n, then it must cross zero.
fplot(rhs(neq))
yline(0)
grid on
So it looks as if a solution exists, roughly a little less than -1. And since fsolve found -1.25, that is it. The only obvious solution has a negative value for n. We can look more carefully for positive n.
fplot(rhs(neq),[0,10])
yline(0)
grid on
fplot(rhs(neq),[5,100])
yline(0)
grid on
There are no solutions in the interval [1,4]. In fact, it appears as if there may never be a positive solution for n.
I would suggest you have a mistake in your equations, in some way. There appears to be no solution that you want to see for the set of constants you have in those equations.
##### 2 CommentiMostra NessunoNascondi Nessuno
Alex Sha il 23 Feb 2024
There ara two solutions as below:
1:
x1: -0.000337662743718998
x2: 0.0675332211134898
x3: -1.25058458523988
2:
x1: 0.000266507513583617
x2: -0.0313538622080008
x3: -8.87840656644425
John D'Errico il 1 Apr 2024 alle 17:57
@Alex Sha - while they may be solutions, they do NOT satisfy the requirements.

Accedi per commentare.

### Categorie

Scopri di più su Calculus in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by