Designing an iterative algorithm for a nonlinear system of equations

4 visualizzazioni (ultimi 30 giorni)
I am trying to design a function root2d2 (with a nested function root2d) that takes an input t and iterates pre-programmed initial values through fsolve to numerically approximate a system solution. There are three nonlinear equations involved with rather nasty coefficients.
function F = root2d2(t)
t_0 = 21.54851793;
x_0 = [20.7936,20.7936,0.0022222222];
fun = @root2d;
x_0 = fsolve(fun,x_0);
while t_0 <= t
x_0 = fsolve(fun,x_0);
t_0 = t_0 + 0.5;
function F = root2d(x)
F(1) = 0.0022222222*(1 - exp(-x(1)*0.95))^(-1) - x(3);
F(2) = 0.0102597835*(1 - 4*exp(-1.9*t_0 + 1.9*x(2)))^(0.5) - x(3);
F(3) = 4000000 + 10000*x(2) - 0.5540166205/x(3)^2 - 1.052631579*(x(2)-x(1))*x(3)^2 - 213157.8947*(-1.578947368 + x(1) + 2.105263158*exp(-0.95*x(1)) - 0.5263157895*exp(-1.9*x(1)));
end
end
However, when attempting to call the function, I receive an error. This is the error message I received after attempting to call the function:
>> fun = root2d2(25);
Error: File: root2d2.m Line: 11 Column: 5
Function definition is misplaced or improperly nested.
Am I calling the function incorrectly, and/or are there syntax errors in my code?

Risposta accettata

Star Strider
Star Strider il 30 Gen 2018
The principal problem is that you are calling the function from within the function.
I cannot figure out what you are doing. This is my best guess to get your code to work:
t = 25;
t_0 = 21.54851793;
x_0 = [20.7936,20.7936,0.0022222222];
k1 = 0;
while t_0 <= t
k1 = k1+1;
x(:,k1) = fsolve(@root2d,x_0);
t_0 = t_0 + 0.5
end
X % Output Result
function F = root2d(x)
F(1) = 0.0022222222*(1 - exp(-x(1)*0.95))^(-1) - x(3);
F(2) = 0.0102597835*(1 - 4*exp(-1.9*t_0 + 1.9*x(2)))^(0.5) - x(3);
F(3) = 4000000 + 10000*x(2) - 0.5540166205/x(3)^2 - 1.052631579*(x(2)-x(1))*x(3)^2 - 213157.8947*(-1.578947368 + x(1) + 2.105263158*exp(-0.95*x(1)) - 0.5263157895*exp(-1.9*x(1)));
end
Make any necessary changes to get it to do what you want.
  1 Commento
Kelly Catlin
Kelly Catlin il 31 Gen 2018
Perhaps I should have explained more thoroughly. I have a model based on the system of three equations (namely, F(1), F(2), and F(3)). For a given input time (t), I wish to find the variables x(1), x(2), and x(3) by iterating reasonably small increments from t_0, the initial time. At t_0 I have reasonable initial guesses for the solution to the system of equations (listed above as x_0). However, I need to iterate from these initial guesses some number of times (in this case, I was arbitrarily using t_0 + 0.5, though of course I could also choose a smaller value for precision) until I approximate the values of x(1), x(2), and x(3) at the desired time t. This could be any value greater than t_0, from 21.5 seconds to something obscene in the hundreds of thousands of seconds.
In short, I need to replug new initial guesses for x_0 and resolve the system for each increment, incrementing from t_0 + 0.5 + 0.5 ... ad nauseam until t is obtained and x_0 represents a reasonable initial guess for solving with t. Then the function should produce the values of x(1), x(2), and x(3) given only a single argument (t) by the user.

Accedi per commentare.

Più risposte (1)

Alex Sha
Alex Sha il 8 Feb 2020
There are two set of solutions:
1
x1: 0.256953706774553
x2: -399.382730699003
x3: 0.0102597835062331
Fevl:
9.92301206481638E-12
-6.23310084102435E-12
4.68173766421387E-9
2:
x1: 20.7935613076643
x2: 20.7934490426555
x3: 0.00222222748385822
Fevl:
-5.27799973995199E-9
6.45898215829608E-6
3.15671786665916E-6

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by