Mathematica8: Finding maximum value of difference of two functions on [-1,1] - graph

I have two approximated functions and I want to find the maximum value (error) between their graphs, to see how much they approach. I used :
FindMaximum[Abs[f[x] - p[x]], x], but Mathematica 8 gave me that output:
{2.75612*10^104, {x -> 2.75612*10^104}}
what does this mean? It is too big!
can you suggest me a better way?
Thanks

It's hard to tell not knowing your functions, but I'd guess that the position of the maximum it found is well outside your intended domain. You may have more success using a different form or FindMaximum, namely
FindMaximum[Abs[f[x] - p[x]],{x,x0,xmin,xmax}]
where x0 would be your initial guess for it (can be any point inside the region of interest), and xmin,xmax are the endpoints of your region of interest.

The reason is probably what Leonid said. To look at what FindMaximum is doing in real time, you can do
f[x_] := Sin[x];
p[x_] := x^2;
lst = {};
Monitor[
FindMaximum[Abs[f[x] - p[x]], x,
EvaluationMonitor :> (AppendTo[lst, x]; Pause[.01])
], ListPlot[lst, PlotRange -> Full]
]
the vertical axis on the resulting plot is the x-coordinate FindMaximum is currently looking at. Once FindMaximum is done, the plot disappears; the list is stored in lst so you can eg ListPlot it.
You can also try this with {Abs[f[x] - p[x]], -1 <= x <= 1} as the argument, as suggested by Spencer Nelson, to see how the search proceeds then.

This is probably caused by some sort of overflow in one of the two functions when the input value of x is a very large number. You should restrict your domain to [-1, 1]:
FindMaximum[{Abs[f[x] - p[x]], -1 <= x <= 1}, x]

If you want to search for a global maximum within the interval {a, b}, I suggest NMaximize:
NMaximize[{Abs[f[x] - p[x]], a <= x <= b}, x].
Note that FindMaximum searches for any local maximum, which is only good if you know that, for your particular function, a local maximum would also be a global maximum.
Instead of the objective function Abs[f[x] - p[x]], you may wish to use the objective function (f[x] - p[x])^2. This would make the objective function smooth (if f[x] and p[x] are smooth), which can help improve the efficiency of some numerical optimization methods.

Related

R: define range with open lower limit and closed upper limit

How to define a range in R of the following form?
(0, 5]
Does the range() function have an option for this?
What would be the best way to pick a random number in a range of the form (x-1, x] given x?
I have done the following so far:
x=1
round(stats::runif(length(x), x-1, x), 2))
I just want to make sure to never obtain 0 here. Thanks!

Mathematica piecewise function bad plot rendering

I wanted to plot a user-defined Piecewise function (pagoda function) in Mathematica 10.2.
It seems straightforward to me unfortunately the easy command leads to a bad result.
My first approach was:
f[x_] := Piecewise[{{0, x <= -1}, {-Abs[x] + 1, -1 < x < 1}, {0,
x >= 1}}]
Plot3D[ 5*f[x]*f[y], {x, -1.5, 1.5}, {y, -1.5, 1.5}]
I also tried to set MaxRecursion which lead to more terrible results in a few cases (e.g. 2,3).
Can anybody tell me how to plot this function in a smooth nice way?
Thanks,
Felix
As far as I can remember, making visible gaps was introduced as a feature. Before that, piecewise or discontinuous functions were plotted like this:
Plot[Piecewise[{{x, x <= 1}, {3, x > 1}}], {x, 0, 3}, Exclusions -> None]
That behavior gives the wrong impression. I would have to check when this was default or if I'm completely off here. Anyway, as already noted in the comments, you can use the Exclusions option to get connected graphs.
You don't need to increase PlotPoints because Mathematica will (hopefully always) recognize the boundaries of the pieces as places where it needs to recursively increase points. Therefore, the MaxRecursion option is far more important to give a smooth plot. This example was rendered with only 10 points, but a recursion value of 5:
Therefore, your function renders extremely well even with 10 plot-points when the recursion is high enough. Look how many subdivisions you get on the cracks
Plot3D[5*f[x]*f[y], {x, -1.5, 1.5}, {y, -1.5, 1.5}, PlotRange -> All,
Exclusions -> None, PlotPoints -> 10, MaxRecursion -> 6, Mesh -> All]
Finally, note that the gaps are not restricted to Piecewise functions. As you can verify yourself, UnitStep will also show gaps. You can try it with your example by using an undocumented function to turn everything to UnitStep:
Simplify`PWToUnitStep[5*f[x]*f[y]]
(*
5 (1 - Abs[x]) (1 - Abs[y]) (1 - UnitStep[-1 - x]) (1 -
UnitStep[-1 + x]) (1 - UnitStep[-1 - y]) (1 - UnitStep[-1 + y])
*)
With all due respect to #halirutan, by itself MaxRecursion set to 6 was not enough in the following plot to adjust the peak values of a piecewise function to be a monotonic increasing sequence:
This improved, when, in addition, I set PlotPoints to 240, as follows.
However, that does not completely solve all the display problems. For example, note in the plots above, that the initial value y = 0, is not correctly plotted as a blue vertical line despite use of Exclusions->None. Moreover, the grid lines, which are Dotted, do not display as dots, but as dashes which run off below the x-axis. All of these problems can be solved, probably more efficiently, by generating the points or dots as list data and using ListPlot or, as in this case ListLogPlot and using Joined->True when appropriate. That is low level solution, but is needed in more complicated plots to obtain a plot with shorter run time and more accessible control over the display features.

Mathematic Plot With Variables That Interact

I am trying to calculate two volumes which are related to each other. In this case as one volume increases it means more of the other volume is possible.
My code is as follows:
Plot[{(6.78966*10^22)(b)},{((9.0226522*10^22)(x))}, {(b, 0, 5.5*10^6),(x, 0, 5.5*10^6)}]
I want this to be plotted on one graph, so it can show the relationship of increasing one volume while the other decreases. However, I can't get this to display in wolfram alpha, a graphing calculator, or mathematica. It seems extremely simple and I am probably just making a dumb error.
The error that is being thrown by mathematica is: ( " cannot be followed by " b,0,5.5*10^6)
But when I try it without the parenthesis it says I do not have enough rules to define my function. Is there a better way to do this?
What I am trying to do is find how many cm^3 of plutonium is needed to convert cm^3 of cadmium. I have done the relationships, but now I am trying to plot it. The maximum volume that can be utilized is 5.5*10^6. So I want one line to end when all of the cm^3 of the volume are cadmium and the other to end when all of the cm^3 is plutonium. This will allow me to find the point in which they intersect optimizing my problem.
Taking the maximum volume, m
m = 5.5*10^6;
Plot[{6.78966*10^22 (m - x), 9.0226522*10^22 x}, {x, 0, m}]
Solve[6.78966*10^22 (m - x) == 9.0226522*10^22 x, x]
{{x -> 2.36165*10^6}}

Interpretation of logn^logn?

How should I interpret
How do I interpret this? One way is to take it as logn(logn) and other is . Both would be giving different answers.
For eg:
Taking base 2 and n=1024, in first case we get 10*10 as ans. In the second case, we get 10^10 as ans or am I doing something wrong?
From a programmer's viewpoing a good way to better understand a function is to plot it at different parts of its domain.
But what is the domain of f(x) := ln(x)^ln(x)? Well, given that the exponent is not an integer, the base cannot be smaller than 1. Why? Because ln(x) is negative for 0 < x < 1 and it is not even defined for x <= 0.
But what about x = 1. Given that ln(1) = 0, we would get 0^0, which is not defined either. So, let's plot f(x) for x between: 1.000001 and 1.1. We get:
The plot reveals that there would be no harm in extending the definition of f(x) at 1 in this way (let me use pseudocode here):
f(x) := if x = 1 then 1 else ln(x)^ln(x)
Now, let's see what happens for larger values of x. Here is a plot between 1 and 10:
This plot is also interesting because it exposes a singular behavior between 1 and 3, so let's plot that part of the domain to see it better:
There are a couple of questions that one could ask by looking at this plot. For instance, what is the value of x such that f(x)=1? Mm... this value is visibly between 2.7 and 2.8 (much closer to 2.7). And what number do we know that is a little bit larger than 2.7? This number should be related to the ln function, right? Well, ln is logarithm in base e and the number e is something like 2.71828182845904.... So, it looks like a good candidate, doesn't it? Let's see:
f(e) = ln(e)^ln(e) = 1^1 = 1!
So, yes, the answer to our question is e.
Another interesting value of x is the one where the curve has a minimum, which lies somewhere between 1.4 and 1.5. But since this answer is getting too long, I will stop here. Of course, you can keep plotting and answering your own questions as you happen to encounter them. And remember, you can use iterative numeric algorithms to find values of x or f(x) that, for whatever reason, appear interesting to you.
Because log(n^log n)=(log n)^2, I would assume that log n^log n should be interpreted as (log n)^(log n). Otherwise, there's no point in the exponentiation. But whoever wrote that down for you should have clarified.

Remove redundant points for line plot

I am trying to plot large amounts of points using some library. The points are ordered by time and their values can be considered unpredictable.
My problem at the moment is that the sheer number of points makes the library take too long to render. Many of the points are redundant (that is - they are "on" the same line as defined by a function y = ax + b). Is there a way to detect and remove redundant points in order to speed rendering ?
Thank you for your time.
The following is a variation on the Ramer-Douglas-Peucker algorithm for 1.5d graphs:
Compute the line equation between first and last point
Check all other points to find what is the most distant from the line
If the worst point is below the tolerance you want then output a single segment
Otherwise call recursively considering two sub-arrays, using the worst point as splitter
In python this could be
def simplify(pts, eps):
if len(pts) < 3:
return pts
x0, y0 = pts[0]
x1, y1 = pts[-1]
m = float(y1 - y0) / float(x1 - x0)
q = y0 - m*x0
worst_err = -1
worst_index = -1
for i in xrange(1, len(pts) - 1):
x, y = pts[i]
err = abs(m*x + q - y)
if err > worst_err:
worst_err = err
worst_index = i
if worst_err < eps:
return [(x0, y0), (x1, y1)]
else:
first = simplify(pts[:worst_index+1], eps)
second = simplify(pts[worst_index:], eps)
return first + second[1:]
print simplify([(0,0), (10,10), (20,20), (30,30), (50,0)], 0.1)
The output is [(0, 0), (30, 30), (50, 0)].
About python syntax for arrays that may be non obvious:
x[a:b] is the part of array from index a up to index b (excluded)
x[n:] is the array made using elements of x from index n to the end
x[:n] is the array made using first n elements of x
a+b when a and b are arrays means concatenation
x[-1] is the last element of an array
An example of the results of running this implementation on a graph with 100,000 points with increasing values of eps can be seen here.
I came across this question after I had this very idea. Skip redundant points on plots. I believe I came up with a far better and simpler solution and I'm happy to share as my first proposed solution on SO. I've coded it and it works well for me. It also takes into account the screen scale. There may be 100 points in value between those plot points, but if the user has a chart sized small, they won't see them.
So, iterating through your data/plot loop, before you draw/add your next data point, look at the next value ahead and calculate the change in screen scale (or value, but I think screen scale for the above-mentioned reason is better). Now do the same for the next value ahead (getting these values is just a matter of peeking ahead in your array/collection/list/etc adding the for next step increment (probably 1/2) to the current for value whilst in the loop). If the 2 values are the same (or perhaps very minor change, per your own preference), you can skip this one point in your chart by simply adding 'continue' in the loop, skipping adding the data point as the point lies exactly on the slope between the point before and after it.
Using this method, I reduce a chart from 963 points to 427 for example, with absolutely zero visual change.
I think you might need to perhaps read this a couple of times to understand, but it's far simpler than the other best solution mentioned here, much lighter weight, and has zero visual effect on your plot.
I would probably apply a "least squares" algorithm to obtain a line of best fit. You can then go through your points and downfilter consecutive points that lie close to the line. You only need to plot the outliers, and the points that take the curve back to the line of best fit.
Edit: You may not need to employ "least squares"; if your input is expected to hover around "y=ax+b" as you say, then that's already your line of best fit and you can just use that. :)

Resources