## bug #38961: fplot needs adaptive scaling algorithm

 Submitted by: None Submitted on: Fri 10 May 2013 05:15:31 PM UTC Category: Plotting Severity: 3 - Normal Priority: 5 - Normal Item Group: Inaccurate Result Status: Confirmed Assigned to: None Originator Name: Mariusz Originator Email: -unavailable- Open/Closed: Open Release: 3.6.3 Operating System: Any

Add a New Comment (Rich Markup):

## You are not logged in

Thu 11 Jul 2013 04:32:20 PM UTC, comment #5:

I overhauled fplot in this changeset (http://hg.savannah.gnu.org/hgweb/octave/rev/b34202b24212). I upgraded it to be more Matlab compatible and I added an additional stopping condition to detect when the algorithm is failing. This works for functions which end up having a relative tolerance that won't converge because the function value at 0 is near 0. However, the real long-term fix is to add an adapative scaling algorithm of the sort used in the quadrature routines. Basically, the current algorithm is no good if the function contains discontinuities. Try

Rik <rik5>
Thu 16 May 2013 02:26:06 PM UTC, comment #4:

I've changed the category as this also occurs for OpenGL plotting.

I tried the example in Matlab R2013a. Matlab's fplot() limits the number of points to 111 for this example to 111.

I've included two more examples for reference.

Ben Abbott <bpabbott>
Fri 10 May 2013 11:34:02 PM UTC, comment #3:

Ok, I looked at this a little deeper. The default tolerance is the same in all cases, 2e-3. Fplot iteratively interpolates the given function over increasing ranges of x until the maximum error falls below that threshold. Your particular function simply never converges, at least using the calculation of error in there now.

I think convergence is failing on certain functions that have very small values near the origin where interpolation is slightly off, such that small absolute errors become large relative errors. Besides your function "x^2 * sin(x)", another example function that doesn't converge is "x^2 + x^4". Another is "(exp(x)-1)^2".

It could be argued that the method for calculating convergence could be improved to fix this problem. Needs input from someone with a stronger mathematical background than mine.

Mike Miller <mtmiller>
Fri 10 May 2013 09:31:41 PM UTC, comment #2:

Why does it use the default tolerance for the first but not the second? It shouldn't be dependent on the function. If it is, it shouldn't change by orders of magnitude by simply subtracting 1 from the function I'm plotting.

I'm creating an example for a tutorial and I'm wondering how to explain the unexpected behavior.

Anonymous
Fri 10 May 2013 05:58:39 PM UTC, comment #1:

Confirmed, but no I don't think it's a bug. The third argument to fplot gives you control over how many points or how accurate the plot should be made. Your second plot is slow because of the particular function you are evaluating and the default tolerance of fplot, not simply because something was changed. If you limit the plot to a fixed number of points you should see no change in speed.

Mike Miller <mtmiller>
Fri 10 May 2013 05:15:31 PM UTC, original submission:

print takes a very long time when changing the plot in a minor way.

octave:1> fplot('x^2*sin(x)+1',[-2 2]),tic,print(1, "a.png","-dpng"),toc
Elapsed time is 0.707911 seconds.

octave:2> fplot('x^2*sin(x)+0',[-2 2]),tic,print(1, "b.png","-dpng"),toc
Elapsed time is 14.3234 seconds.

I'm not complaining, I just think it's very very odd. I get this behavior consistently. Can anyone offer an explanation if this isn't a bug?

Anonymous

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):

Comment:

No files currently attached

Depends on the following items: None found

Items that depend on this one: None found

Carbon-Copy List
• -unavailable- added by rik5 (Posted a comment)
• -unavailable- added by bpabbott (Posted a comment)
• -unavailable- added by mtmiller (Posted a comment)
• -unavailable- added by None (Submitted the item)
•

Do you think this task is very important?
This task has 0 encouragements so far.

Only project members can vote.

Please enter the title of George Orwell's famous dystopian book (it's a date):