3D Dome in gnuplot with sqrt(x) function - function

I have to "draw" a 3d function in gnuplot with the function sqrt(1-x**2+y**2), where the graph looks like a dome.
I am supposed to do it with unset parametric.
I just couldnt find out how to do it, any help would be appreciated.

First of all, your function sqrt(1-x**2+y**2) doesn’t represent the dome shape. The correct formula would be sqrt(1-(x**2+y**2)).
Then, even if you specify unset parametric, you can draw a dome shape by giving the function form to splot.
unset parametric
set xrange [-1.2:1.2]
set yrange [-1.2:1.2]
set isosamples 20,20
splot real(sqrt(1-(x**2+y**2))), sqrt(1-(x**2+y**2))
Many of the mathematical functions in gnuplot support complex numbers. sqrt(x) returns a complex number if x is negative. Note that such a value is treated like a missing value on a plot.
This script generates a figure like this (on pngcairo terminal).

Related

In octave, error: for x^A, A must be a square matrix. Use .^ for elementwise power

In Octave I defined a function in a separate file square.m
function y = square(x)
y = x^2;
endfunction
In other file script.m I have
disp("Hello World 2");
fplot( #(x) square(x),[-1 1])
And I get
error: for x^A, A must be a square matrix. Use .^ for elementwise power.
Also if I try
y = x.^2;
inside the function I get the exact same message
The reason you're getting that error is because fplot is passing the range you specified all at once as a vector, treating your function as a vectorised function, expecting a vector input and returning a vector output.
You can confirm this by turning "debug on error" to true, by doing debug_on_error(true), run the offending line, and inspect x.
Therefore, inside your function, things go wrong, because you're trying to get the square of a vector, which is an illegal operation (mathematically speaking).
Converting your function to y = x.^2 should work in this case, because you'd be converting each element of the vector to its square, which is what you want. But obviously, simply changing ^ to .^ might not work for every problem.
In general, it's better to create your own 'range' and 'outputs' and plot them directly using plot; this gives you far more control, and you can inspect the inputs and outputs first to ensure you're plotting what you think you're plotting.
Welcome to StackOverflow!
I have just tried your code on https://octave-online.net/ (no need to create an account nor even the files).
The second version works "as expected": y = x .^ 2; inside the function.
Make sure you saved the file after the modification?

Get right-hand side of equation

I am calling the function mnewton(0=expr, alpha, %pi/4) to get the root of a rather complex equation expr.
%(i1) mnewton(0=expr, alpha, %pi/4)
%(o1) [alpha=0.678193754078621]
I need to apply another function to this result (e.g. sin) and then want to plot it. Just linking the functions does not work:
%(i2) sin(mnewton(0=expr, alpha, %pi/4)[1])
%(o2) sin(alpha=0.678193754078621)
This is because the expression alpha=0.678193754078621 is not a number. How do I convert alpha=0.678193754078621 to just 0.678193754078621?
I can't just copy the numerical value and add it manually as I want to plot this and my expr will have a different root for each y.
The function rhs(expr) does exactly that.
Check the manual for more information on this.

Is it possible to plot complex variable in wxMaxima or Octave?

For example , if I want to plot Sin(z) where z is a complex variable , how I will achieve it in either Octave or Maxima?
I don't know about Octave, but here is a message about that, with some code you can try in Maxima: https://www.ma.utexas.edu/pipermail/maxima/2007/006644.html
There may be more specific information for wxMaxima -- you can try their user forum: https://sourceforge.net/p/wxmaxima/discussion/435775/
(referring Octave 4.0.0)
How do you want to try to represent the output of the function? Plotting either the real or imaginary parts of the output can be done fairly simply using a 3-dimensional graph, where the x and y axes are the real and imaginary components of z, and the vertical axis is either the real or imaginary values of sin(z). Producing those are fairly simple in Octave. Here's a link to a script you can save and run to show an example.
Simply change the g = exp(f) line to g = sin(f).
Octave-help mailing list example
Note that the imaginary part plot is commented out. Just switch the # between the different plot commands if you want to see that part.
Now, are you instead looking for options to map the Z plane (z=x+iy) to the W plane (w=u+iv) and represent closed contours mapped by w=sin(z)? in that case you'll need to do parametric plotting as described on this FIT site. There is a link to his Matlab program at the bottom of the explanation that provides one method of using color coding to match z->w plane contour mapping.
Those m-files are written for Matlab, so a few things do not work, but the basic plotting is compatible with Octave 4.0.0. (the top level ss13.m file will fail on calls to flops and imwrite)
But, if you put your desired function in myfun13.m for f, df and d2f, (sin(z), cos(z), -sin(z) respectively), then run cvplot13, you'll get color maps showing the correspondence between z and w planes.
wxMaxima has a plot3d that can do it. Since the expression to plot is in terms of x and y, I plotted the function's magnitude with abs(f(x+%i*y)):
plot3d(abs((x+%i*y-3)*(x+%i*y-5)*(x+%i*y-6)), [x,2,7], [y,-1,1], [grid,100,100], [z,0,5])$

Reuse smoothed function plotted with gnuplot

I have a data file that I plot using
plot 'data.dat' u 1:2 smooth csplines
and I want to reuse this function to get the ratio with another data file function. The x points for the two functions are different so I need to have the smoothed function with the entire information over any x-point to construct the ratio at an arbitrary x. Is possible to define the plotted smoothed function as a function over reals?
You can save the smoothed functions to an external file. You must make sure that both smoothed functions are interpolated over the same xrange and with the same number of samples:
set samples 500
set xrange [] writeback
set table 'data-smoothed.dat'
plot 'data.dat' using 1:2 smooth csplines
unset table
set xrange restore
set table 'data2-smoothed.dat'
plot 'data2.dat' using 1':2 smooth csplines
unset table
plot '< paste data-smoothed.dat data2-smoothed.dat' using 1:($4/$2)
If you're working on Windows, you may want to use the litte script paste.py shown in Get ratio from 2 files in gnuplot to merge the files.

matlab function which is a function of an intergral

I need to write my own function which has the form f(x,y)=Integrate(g(x,y,z),z from 0 to inf). so the code I used was:
function y=f(x,y)
g=#(z)exp(-z.^2)./(z.^x).*(z.^2+y.^2).^(x/2);% as a function of x,y and z
y=quadgk(g,0,inf)
and if I call it for a single value like f(x0,y0), it works but if I try to calculate something like f([1:10],y0), then the error message says that there is something wrong with the times and dimension. In principle I can use for loops but then my code slows down and takes forever. Is there any help I can get from you guys? or references?
I'm trying to avoid the for loop since in matlab it's much faster to use matrix computation than to use for loop. I wonder if there is any trick that I can take advantage of this feature.
Thanks for any help in advance,
Lynn
Perhaps you can try to transpose the interval, creating row based values instead of column based f([1:10]',y0). Otherwise something in your function might be wrong, for example to get x^y to work with lists as input, you have to prefix with a dot x.^y. The same for mulitply and division I think..
If loop is no problem for you, you should do something like:
function y2=f(x,y)
y2=zeros(size(x));
for n=1:numel(x)
g=#(z)exp(-z.^2)./(z.^x(n)).*(z.^2+y.^2).^(x(n)/2);% as a function of x,y and z
y2(n)=quadgk(g,0,inf)
end
The problem here is that quadk itself uses vectors as argument for g. Then you have in g somethink like z.^x, which is the power of two vectors that is only defined if z and x have the same dimension. But this is not what you want.
I assume that you want to evaluate the function for all arguments in x and that the output vector has the same dimension as x. But this does not seem to be possible since even this simple example
g=#(x)[x;x.^2]
quad(g,0,1)
does not work:
Error using quad (line 79)
The integrand function must return an output vector of the same length as the
input vector.
A similar error shows when using quadgk. The documentation also says that this routine works only for scalar functions and this is not surprising since an adaptive quadrature rule would in general use different points for each function to evaluate the integral.
You have to use quadvinstead, which can integrate vector valued functions. But this gives wrong results since your function is integrated in the interval [0,\infty).