ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 22 Oct 2018 11:25:51 -0500Trouble finding intersection of two functionshttps://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/ Hi all,
I'm still pretty new using SageMath, but I'm trying to duplicate functionality that I've been able to do in wolfram alpha
Wolfram Input:
Intersection points of [//math:90000 1.03^x//] and [//math:63000 1.095^x//]
So far I've been able to recreate and graph these functions very easily using SageMath, but I'm having a difficult time using the solve function to actually return a numerical value for the intersection point itself.
My SageMath code looks like:
x = var('x')
f1 = (63000*((1.095)^x))
f2 = (90000*((1.03)^x))
ans=solve(f2==f1,x)
print ans
print n(ans[0].rhs())
ans prints as
"[
219^x == 1/35*200^(x - 1)*103^x*100^(-x + 2)
]"
And I get an error "TypeError: cannot evaluate symbolic expression numerically" in my attempts to resolve it to an approximate number.
Can anyone tell me what I'm doing wrong?Mon, 22 Oct 2018 00:51:55 -0500https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/Comment by lgushurst for <p>Hi all,</p>
<p>I'm still pretty new using SageMath, but I'm trying to duplicate functionality that I've been able to do in wolfram alpha</p>
<p>Wolfram Input: </p>
<pre><code>Intersection points of [//math:90000 1.03^x//] and [//math:63000 1.095^x//]
</code></pre>
<p>So far I've been able to recreate and graph these functions very easily using SageMath, but I'm having a difficult time using the solve function to actually return a numerical value for the intersection point itself.</p>
<p>My SageMath code looks like:</p>
<pre><code>x = var('x')
f1 = (63000*((1.095)^x))
f2 = (90000*((1.03)^x))
ans=solve(f2==f1,x)
print ans
print n(ans[0].rhs())
</code></pre>
<p>ans prints as
"[
219^x == 1/35<em>200^(x - 1)</em>103^x*100^(-x + 2)
]"</p>
<p>And I get an error "TypeError: cannot evaluate symbolic expression numerically" in my attempts to resolve it to an approximate number.</p>
<p>Can anyone tell me what I'm doing wrong?</p>
https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?comment=44022#post-id-440221. It's not homework, I'm just learning SageMath
2. I didn't think x needed to be part of a set, there's only one intersection point and it's at roughly ~5.82. Are you saying I need to set this problem up in another way?Mon, 22 Oct 2018 07:45:40 -0500https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?comment=44022#post-id-44022Comment by Emmanuel Charpentier for <p>Hi all,</p>
<p>I'm still pretty new using SageMath, but I'm trying to duplicate functionality that I've been able to do in wolfram alpha</p>
<p>Wolfram Input: </p>
<pre><code>Intersection points of [//math:90000 1.03^x//] and [//math:63000 1.095^x//]
</code></pre>
<p>So far I've been able to recreate and graph these functions very easily using SageMath, but I'm having a difficult time using the solve function to actually return a numerical value for the intersection point itself.</p>
<p>My SageMath code looks like:</p>
<pre><code>x = var('x')
f1 = (63000*((1.095)^x))
f2 = (90000*((1.03)^x))
ans=solve(f2==f1,x)
print ans
print n(ans[0].rhs())
</code></pre>
<p>ans prints as
"[
219^x == 1/35<em>200^(x - 1)</em>103^x*100^(-x + 2)
]"</p>
<p>And I get an error "TypeError: cannot evaluate symbolic expression numerically" in my attempts to resolve it to an approximate number.</p>
<p>Can anyone tell me what I'm doing wrong?</p>
https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?comment=44021#post-id-440211. Is this homework ?
2. What set $x$ is supposed to belong to ?Mon, 22 Oct 2018 07:37:05 -0500https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?comment=44021#post-id-44021Answer by Emmanuel Charpentier for <p>Hi all,</p>
<p>I'm still pretty new using SageMath, but I'm trying to duplicate functionality that I've been able to do in wolfram alpha</p>
<p>Wolfram Input: </p>
<pre><code>Intersection points of [//math:90000 1.03^x//] and [//math:63000 1.095^x//]
</code></pre>
<p>So far I've been able to recreate and graph these functions very easily using SageMath, but I'm having a difficult time using the solve function to actually return a numerical value for the intersection point itself.</p>
<p>My SageMath code looks like:</p>
<pre><code>x = var('x')
f1 = (63000*((1.095)^x))
f2 = (90000*((1.03)^x))
ans=solve(f2==f1,x)
print ans
print n(ans[0].rhs())
</code></pre>
<p>ans prints as
"[
219^x == 1/35<em>200^(x - 1)</em>103^x*100^(-x + 2)
]"</p>
<p>And I get an error "TypeError: cannot evaluate symbolic expression numerically" in my attempts to resolve it to an approximate number.</p>
<p>Can anyone tell me what I'm doing wrong?</p>
https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?answer=44024#post-id-44024 It's not homework, I'm just learning SageMath
Okay. If you lied, may you live in interesting times (e. g. cross-examination by your instructor...)
I didn't think x needed to be part of a set,
Really ? That means that we'll work in an "elementary" (high school) point of view, and consider searching for real solutions.
Being lazy, I'll start by minimizing typing. Store your equation in a Python variable :
sage: E=90000*1.03^x==63000*1.095^x ; E
90000*1.03000000000000^x == 63000*1.09500000000000^x
Try to solve it directly :
sage: E.solve(x)
[219^x == 1/35*200^(x - 1)*103^x*100^(-x + 2)]
Sage tells us that there may exist a solution, which would satisfy this equation, which is, in some way, a restatement of our original equation (sage replaced your floating-pont numbers by close approximations expressed as "simple" fractions, an interesting step I won't discuss here...). This implicit solution isn't good enough for us.
Let's try to get rid of the powers ; to this end, we'll take the logarithms of both members of the equation, thus mapping our powers to products (see [Wikipedia](https://en.wikipedia.org/wiki/Logarithm) for an explanation if needed). The logarithm being monotonic on the set of positive real numbers, this will get you the same solution(s), if any.
Sage allows us to shortcut that by applying the `log` method to the equation :
sage: E.log().solve(x)
[log(9*103^x*100^(-x + 2)) == log(315*219^x*200^(-x + 1))]
Well... this worked only partially : Sage did not expand the logarithms of both sides, and still leaves us with an implicit equation. Let's do it ; again, Sage allows us to apply the `log_expand` method to both sides of the equation :
sage: E.log().expand_log()
0.02955880224154443*x + log(90000) == 0.09075436326846412*x + log(63000)
Now, this is a first degree linear equation, that Sage *can* solve :
sage: E.log().expand_log().solve(x)
[x == 91739056/5614023*log(90000) - 91739056/5614023*log(63000)]
Sage tells us that if finds one solution, which it gives us as "exactly" as it can. Oh, you were expecting a numerical approximation of the value ? Here it comes :
sage: E.log().expand_log().solve(x)[0].rhs().n()
5.82844470993300
Now, is this solution *the* solution ? Maybe not. You see, there are more in maths than *real* numbers ; $x$ may be a [complex number](https://en.wikipedia.org/wiki/Complex_number), whose [logarithm](https://en.wikipedia.org/wiki/Logarithm#Complex_logarithm) is a horse of a totally different color.
But discussing the nature and number of solution(s) of your equation is left to you, dear reader.
But beware : to be rigorous, this discussion needs sophomore-level analysis. Logarithms of real numbers were more or less understood by the beginning of the XVIIth century, while correct comprehension of complex analysis had to wait for the XIXth century, and is closely related to still open problems.Mon, 22 Oct 2018 09:25:36 -0500https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?answer=44024#post-id-44024Comment by lgushurst for <pre><code>It's not homework, I'm just learning SageMath
</code></pre>
<p>Okay. If you lied, may you live in interesting times (e. g. cross-examination by your instructor...)</p>
<pre><code>I didn't think x needed to be part of a set,
</code></pre>
<p>Really ? That means that we'll work in an "elementary" (high school) point of view, and consider searching for real solutions.</p>
<p>Being lazy, I'll start by minimizing typing. Store your equation in a Python variable :</p>
<pre><code>sage: E=90000*1.03^x==63000*1.095^x ; E
90000*1.03000000000000^x == 63000*1.09500000000000^x
</code></pre>
<p>Try to solve it directly :</p>
<pre><code>sage: E.solve(x)
[219^x == 1/35*200^(x - 1)*103^x*100^(-x + 2)]
</code></pre>
<p>Sage tells us that there may exist a solution, which would satisfy this equation, which is, in some way, a restatement of our original equation (sage replaced your floating-pont numbers by close approximations expressed as "simple" fractions, an interesting step I won't discuss here...). This implicit solution isn't good enough for us.</p>
<p>Let's try to get rid of the powers ; to this end, we'll take the logarithms of both members of the equation, thus mapping our powers to products (see <a href="https://en.wikipedia.org/wiki/Logarithm">Wikipedia</a> for an explanation if needed). The logarithm being monotonic on the set of positive real numbers, this will get you the same solution(s), if any.</p>
<p>Sage allows us to shortcut that by applying the <code>log</code> method to the equation :</p>
<pre><code>sage: E.log().solve(x)
[log(9*103^x*100^(-x + 2)) == log(315*219^x*200^(-x + 1))]
</code></pre>
<p>Well... this worked only partially : Sage did not expand the logarithms of both sides, and still leaves us with an implicit equation. Let's do it ; again, Sage allows us to apply the <code>log_expand</code> method to both sides of the equation :</p>
<pre><code>sage: E.log().expand_log()
0.02955880224154443*x + log(90000) == 0.09075436326846412*x + log(63000)
</code></pre>
<p>Now, this is a first degree linear equation, that Sage <em>can</em> solve :</p>
<pre><code>sage: E.log().expand_log().solve(x)
[x == 91739056/5614023*log(90000) - 91739056/5614023*log(63000)]
</code></pre>
<p>Sage tells us that if finds one solution, which it gives us as "exactly" as it can. Oh, you were expecting a numerical approximation of the value ? Here it comes :</p>
<pre><code>sage: E.log().expand_log().solve(x)[0].rhs().n()
5.82844470993300
</code></pre>
<p>Now, is this solution <em>the</em> solution ? Maybe not. You see, there are more in maths than <em>real</em> numbers ; $x$ may be a <a href="https://en.wikipedia.org/wiki/Complex_number">complex number</a>, whose <a href="https://en.wikipedia.org/wiki/Logarithm#Complex_logarithm">logarithm</a> is a horse of a totally different color.</p>
<p>But discussing the nature and number of solution(s) of your equation is left to you, dear reader.</p>
<p>But beware : to be rigorous, this discussion needs sophomore-level analysis. Logarithms of real numbers were more or less understood by the beginning of the XVIIth century, while correct comprehension of complex analysis had to wait for the XIXth century, and is closely related to still open problems.</p>
https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?comment=44026#post-id-44026I've accepted your answer as the solution and really appreciate the explanation, it's incredibly thorough and helped me understand the context of what I was attempting to do more clearly.Mon, 22 Oct 2018 11:25:51 -0500https://ask.sagemath.org/question/44019/trouble-finding-intersection-of-two-functions/?comment=44026#post-id-44026