Plot of a function from x=0 to x=1 is not shown

Hi all,

I tried to plot a function \frac{1}{(x-1)^{2/3}} but it can only plot the function from x=1 to the right. There should also be the plot from from x=0 to x=1 toward infinity.

This is the suppose to be function plot:

This is my code to plot with JULIA:

using Plots, LaTeXStrings, Plots.PlotMeasures # ignore any warnings 
gr()

f(x) = 1/(x-1)^(2/3)

plot(f,-1,6; color=:blue, xlims=(-1,6), xtick=-1:1:6, yaxis=false,ylims=(-1,10), 
	framestyle=:zerolines, label=L"y = f(x) = \frac{1}{(x-1)^{2/3}}",
	top_margin=10mm, legend=:topright)

I also try to use matplotlib by calling from C++ and it is the same,

// g++ main.cpp -o main -std=c++11 -I/usr/include/python3.9 -lpython3.9 -DWITHOUT_NUMPY
// g++ main.cpp -o main -std=c++11 -lpython3.9 -I/usr/include/python3.9 -I/usr/lib/python3.9/site-packages/numpy/core/include

#include "matplotlibcpp.h"
#include <cmath>

double division(double x, double y)
{
	return x/y;
}
namespace plt = matplotlibcpp;
int main()
{
	// Prepare data.
	int n = 1000;
	std::vector<double> x(n), y(n), z(n);
	for(int i=0; i<n; ++i) 
	{
		x.at(i) = i;
		y.at(i) = pow(i-1,-division(2,3));
	}

	// Set the size of output image to 1200x780 pixels
	plt::figure_size(1200, 780);
	// Plot line from given x and y data. Color is selected automatically.
	plt::plot(x, y);
	// Plot a line whose name will show up as "x exp(-x)" in the legend.
	plt::named_plot("1/(x-1)^(2/3)", x, y);
	// Set x-axis to interval [0,10]
	plt::xlim(0, 10);
	// Add graph title
	plt::title("1/(x-1)^(2/3)");
	// Enable legend.
	plt::legend();
	plt::show();
	// Save the image (file format is determined by the extension)
	// plt::save("./basic.png");
}

and also with gnuplot:

// Compile it with:
//   g++ -o main main.cpp -lboost_iostreams 

#include <vector>
#include <cmath>
#include <utility>
#include <boost/tuple/tuple.hpp>

#include "gnuplot-iostream.h"

int main() {
	Gnuplot gp;

	// Don't forget to put "\n" at the end of each line!
	gp << "set xrange [-2:4]\nset yrange [0:5]\n";
	// '-' means read from stdin.  The send1d() function sends data to gnuplot's stdin.
	gp << "f(x) = 1/(x-1)**(0.666667)\n";
	
	gp << "plot f(x) title 'f(x) = 1/(x-1)^{2/3}'\n";

	
	return 0;
}

It seems even matplotlib and gnuplot cannot plot the function from x=0 to x=1

I accidentally hit tabs and it is posted before I finish writing. Sorry.

1 Like

The problem is that exponentiation with a float exponent is only defined for a strictly positive non-negative base:

julia> f(0.5)  # involving (-0.5)^(2/3)
ERROR: DomainError with -0.5:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
...

If you just rewrite f as

f(x) = (1/(x-1)^2)^(1/3)

then you won’t run into any issues.

3 Likes

Wow thanks a lot, I just learn it now solving this needs only to rewrite f.

1 Like

Or alternatively use the cbrt function, which allows negative arguments and is also probaby more efficient than generic exponentiation y^(1/3), e.g. f(x) = 1/cbrt(x)^2

For the record, Plots.jl seems to do the right thing. For values x<1, the original function is not properly defined, the fractional power has complex solutions, and the function is multivalued.