Get awnsers with infinity using matlab solver

2 views (last 30 days)
I want to get awnsers form the solver where infity is alowed with for example the following formulas (The values that will be inf are not known before hand):
Formulas: [1/S_i1 + 1/100 == 1/100, 1/S_i2 + 1/S_o2 == 1/200, 300 == S_i1 + S_o2]
The variables where made using syms
I expected the following awsers:
S_i1=inf
S_o2=-inf
S_i2=200
It doesnt seem to work with the solve function. What are the other options?
  2 Comments
Rens
Rens on 6 Sep 2024
Hello Torsten,
Thank you for your fast response.
I would expect the first formula to give Si_1​=inf. Then I would like the third formula to be solved as a limit like so:
>> limit(300-S_i1,S_i1,inf)
ans =
-Inf
Then S_i2 should be 200 following the second formula.
Now when i try to solve this it will give no awnser:
>> solve(formulas)
ans =
struct with fields:
S_i1: [0×1 sym]
S_i2: [0×1 sym]
S_o2: [0×1 sym]

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 6 Sep 2024
Edited: John D'Errico on 6 Sep 2024
No. I'm sorry, but you are wrong. You think of infinity as a number. It is not. Infinity is greater than any number. And, yes, MATLAB sometimes returns a result as inf.
1/0
ans = Inf
When that happens, as I say below, think of this as essentially an error flag, but a soft one, that your code is producing something with no valid result, but it is larger than any number we can represent.
Arguably, inf is a limit, not really a number.
But what did you do? I'll just look at the first equation you want to solve.
syms S_i1
solve(1/S_i1 + 1/100 == 1/100,S_i1)
ans = Empty sym: 0-by-1
There is no real number S_i1 that solves the first relation. So MATLAB properly returns an empty result, an indication the problem has no solution in the set of real numbers. With some mental effort that is not really worth our time, I could probably write that same equation as a limit, where the result could be argued to be inf.
syms u
limit(solve(1/S_i1 + 1/100 == u + 1/100,S_i1),u,0,'right')
ans = 
As you can see, solve does not think of infinity as a result. I had to trick it, to generate a result, such that the limit, when u approaches zero from the right, would be seen as inf.
Next, you seem to think that the difference of two infinities can be computed. Again, it cannot, and for good mathematical reasons.
x1 = inf;
x2 = inf + 200;
x2 - x1
ans = NaN
inf-inf is properly NaN, thus Not a Number, because it can be ANYTHING. This next also fails:
2*inf - inf
ans = NaN
Still a NaN.
Think of inf (and NaN) in MATLAB as sort of soft error returns, NaN being a little harder than Inf. inf is something greater than any number you can represent. NaN is just Not a Number. So a NaN results when you could argue for virtually ANY solution.
sin(inf)
ans = NaN
Personally, I would strongly argue 0^0 should return NaN, but I can accept the argument to allow it to return 1 in MATLAB, even though it is technically wrong.
As soft error flags, they can help you to identify a problem, as only certain things produce infs. 1/0, exp(10000), the list is long, but once you learn the kind of things that result in an inf, you will be ahead of the game. The nice thing about inf as a soft error return, is it sometimes can allow you to survive.
1 + 1/inf
ans = 1
So it is a soft error, in that you still get the result you will like to see. But NaNs just propagate like wire coat hangers. And both inf and NaN have valid uses in MATLAB code. I use both of them frequently.
  2 Comments
Rens
Rens on 6 Sep 2024
Hello John,
Thank you for you awnser. I understand what you mean and I understand what infinity represents. I want it to solve it like a limit problem like the statement of the first formula is true for the limit where S_i1 goes to inf. Or when no real number exists, like in this case, the limit case would be checked. So it will give an infinity as awnser.
John D'Errico
John D'Errico on 6 Sep 2024
Edited: John D'Errico on 6 Sep 2024
As I tried to say, solve does NOT think in terms of limits. Does a solution exist? Yes, or no. This is all solve sees. Yes or no, and if yes, to return the solution. Solve cannot know that you want a limit. And there is no capability in solve to recognize you really want a limit. (At least not in this release of MATLAB. Maybe in the future, when our computers will be godlike beings, and they can read our minds to know what we want instead of what we say. I rightly fear that day though.)
Can you use the trick I showed above? Well, yes. but that is a literal hack, based on my knowing what solution I want to see. And I am sure I could find infinitely many ways to hack those equations to produce the result you want.
syms S_i1 S_o2 S_i2
syms u
sol = solve(1/S_i1 + 1/100 == 1/100 + u, 1/S_i2 + 1/S_o2 == 1/200 + u, 300 == S_i1 + S_o2 + u,[S_i1,S_o2,S_i2])
sol = struct with fields:
S_i1: 1/u S_o2: -(u^2 - 300*u + 1)/u S_i2: (200*(u^2 - 300*u + 1))/(200*u^3 - 59999*u^2 + 100*u + 1)
limit(sol.S_i1,u,0,'right')
ans = 
limit(sol.S_o2,u,0,'right')
ans = 
limit(sol.S_i2,u,0,'right')
ans = 
200
So I hacked your equations in such a way that a valid solution always exists, because I know the result I want to see, then I turned it into a set of limits. Is that a good thing? SHUDDER. I would be reluctant to call it a good way to do mathematics. But whatever floats your boat. Let me say I would not want to write a technical paper, and try to slip that past any awake referees based on this result. I might get it through, but I would have an argument on my hands. Hey, the good news is, many technical journals seem not to worry about referees anymore (or is that bad news?)

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!