Tetrahedralization visualization Octave - octave

Hi does anyone know how to visualize the tetrahedrons in the output of the delaunay3() function in Octave?
http://www.obihiro.ac.jp/~suzukim/masuda/octave/html3/octave_151.html
In MATLAB this visualization is done with the tetramesh() function but Octave does not have this function built in!
The link does mention the triplot and trimesh functions but they only create triangles, not tetrahedra.

Unfortunately, I don't know enough about this to give any thoughts on it. But, I would like to mention the following, in case you hadn't seen it before. There have been discussions on tetramesh before. A quick solution was written up by martin_helm:
function tetramesh( T, X, C)
if nargin < 3
C = mod((1:size(T, 1))'-1, size(colormap(), 1) + 1);
endif
triang = [T(:, 1) T(:, 2) T(:, 3); ...
T(:, 2) T(:, 3) T(:, 4); ...
T(:, 3) T(:, 4) T(:, 1); ...
T(:, 4) T(:, 1) T(:, 2)];
patch("Faces", triang, "Vertices", X, "FaceVertexCData", [C; C; C; C])
endfunction
Along with some example usage:
backend("fltk") % backend("gnuplot") could also be used
d = [-1 1];
[x,y,z] = meshgrid(d,d,d);
x = [x(:);0];
y = [y(:);0];
z = [z(:);0];
tetra = delaunay3(x,y,z);
X = [x(:) y(:) z(:)];
tetramesh(tetra, X)
view(30,30)

Related

Functions as arguments in gnuplot

In gnuplot, is there a way to pass a user defined function as an argument to another user defined function? For example, I can write a function loop which will sum shifts of a given function:
f(x) = (x <= 0) ? 0 : 1/(1+x)**2
loop(x, i, s) = (i == 0) ? f(x) : loop(x-s, i-1, s) + f(x)
Then I can do things like:
plot loop(x, 10, 1)
But, how do I define a function loop2 that does this for any function, as in something like:
loop2(g, x, i, s) = (i == 0) ? g(x) : loop2(g, x-s, i-1, s) + g(x)
so that I can then do things like:
f3(x) = (x <= 0) ? 0 : 1/(1+x)**3
plot loop2(f, x, 10, 1)
replot loop2(f3, x, 10, 1)
I think this is not possible in gnuplot 5.4.
The development version (gnuplot 5.5) has recently gained the ability to label a block of text commands as a named executable function, known as a "function block". This gives you access to commands in a function block that are not possible in a one-line user defined function. Here is your example run in a recent build of the development version. At the top level the name of the function ("f" or "f3") is passed as a parameter that can be used to construct a call of the function itself.
function $loop2(name, x, i, s) << EOF
local temp = 0
eval sprintf("temp = %s(x)", name)
return (i == 0) ? temp : temp + $loop2(name, x-s, i-1, s)
EOF
f(x) = (x <= 0) ? 0 : 1/(1+x)**2
f3(x) = (x <= 0) ? 0 : 1/(1+x)**3
set key left Left reverse
set tics nomirror
set border 3
set xrange [0:10]
set yrange [0:1.5]
plot $loop2("f", x, 10, 1), $loop2("f3", x, 10, 1)
And here is a link to an example in the demo collection that illustrates calling one function block from another, wrapping both in a top-level user defined function.
function_block demo

How to solve the Error: x undefined in octave

I was trying to plot data.
Firstly, I have load the data from a file
data = load('ex1data1.txt'); % read comma separated data
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
Then i have called the function plotData
function figure=plotData(x, y)
figure; % open a new figure window
if(is_vector(x) && is_vector(y))
figure=plot(x,y,'rx',MarkerSize,10);
xlabel('Profit in $10,000s');
ylabel('Population of city in 10,000s');
endif
endfunction
But Iam getting an error. which says:
x is undefined
Thank you in advance.
The problem is in the following statement:
X = data(:, 1); y = data(:, 2);
you have defined X variable but when you call
plotData(x, y)
you are using lowercase X
I think if change the statement: plotData(X, y) will solve your problem

Scilab plotting functions with if

I have a problen in scilab
How can I plot functions containing if and < like
function y = alpha(t)
if (t < 227.8) then
y = 0.75;
elseif (t < 300) then
y = 2.8 - 0.009 .* t;
else
y = 0.1;
end
endfunction
and
function [r]=minus_alpha(t)
r = 1 - alpha(t)
endfunction
When I use
x = linspace(0,300)
plot(x, alpha(x))
I got the error message
WARNING: Transposing row vector X to get compatible dimensions
plot2d: falsche Größe für Eingangsargument: inkompatible Größen.
Error 999 : in plot2d called by plot
Sorry for german mix. Thank you.
You can avoid explicit loop and be more efficient using the followin code
function y = alpha(t)
y=0.1*ones(t);
y(t<227.8)=0.75;
i=t>=227.8&t<300;
y(i)=2.8 - 0.009 .* t(i);
endfunction
It is really sad to see a great majority of Scilab community is not aware of vectorized operations. You can change your function to:
function y = alpha(t)
y = 0.1;
if t < 227.8 then
y = 0.75;
elseif t < 300 then
y = 2.8 - 0.009 * t;
end
y = 1 - y;
endfunction
and then use feval to broadcast the function over the sequence:
x = linspace(0, 300);
plot2d(x, feval(x, alpha));
which results:
rule of thumb if you are using for loop you need to revise your code and if someone is offering you a code where there is unnecessary for loop you shouldn't probably use it.
All the proposed answers are overcomplicated considering that the function alpha in the original demand is piecewise-affine. In Scilab in can be coded that way:
x = linspace(0,400,1000);
plot(x,linear_interpn(x,[227.8 300],[0.75 0.1]))
i.e. you just have to know the nodes coordinates (here abscissae) and value of the function at nodes. The function linear_interpn does also multilinear interpolation, it is worth knowing it guys...
If you check the output of your alpha(x), you will see that it is just a scalar (not a vector). I guess you wanted something like this, so it's necessary to iterate through t to compute each value of y based on the value of t:
clc;
clear;
function y = alpha(t)
for i=1:size(t,"*")
if t(i) < 227.8 then
y(i) = 0.75;
elseif t(i) < 300 then
y(i) = 2.8 - 0.009 * t(i);
else
y(i) = 0.1;
end
end
endfunction
x = linspace(0,300);
plot2d(x,alpha(x));
If you find the answer useful, please do not forget to accept it, so others will see that your problem is solved.
Before your answers (thank you) my workaround was a combination of indicator functions composed with floor and exp( -t^2):
function y = alpha(t)
y = floor(exp(-(t .* (t-T1)) / (T1*T1))) * 0.75
+ floor(exp(-((t-T2) .* (t- T1) / (2000)))) .* (2.8-0.009 .* t)
+ floor(exp(-((t-T2) .* (t-1000) / (200000))))*0.1
endfunction

Find ALL local maxima of a function

Hello everybody I try to find ALL the maxima of a function and plot the points where the maxima are.
The Function is:
U[x_,y_,a_]:=-((1-a)/Sqrt[(x-a)^2+y^2])-a/Sqrt[(x+1-a)^2+y^2]- 0.5*(x^2+y^2);
Of course this is a 3D function but i satisfied also if someone give me a code to find all
maxima with the 2D version of the function. I put a graph of the function.
points1=Table[{x, 2*U[x, 0, a]}, {x, -1.5, 1.5,0.005}];
ListPlot[points1, Joined->True,PlotRange->{{-1.5,1.5},{-5.5,-3.0}},
AxesLabel->{"x","cost. di Jacobi"}]
I saw a similar post but with a more complex function so i can't understand how modify the code: https://mathematica.stackexchange.com/questions/5575/how-to-find-all-the-local-minima-maxima-in-a-range .
Some one can help me? Thanks.
i finally solve the 2d problem with maximize. Take a look of how i solve the problem !
a=0.23;
J=-3.5;
U[x_,y_,a_]:=-((1-a)/Sqrt[(x-a)^2+y^2])-a/Sqrt[(x+1-a)^2+y^2]- 0.5*(x^2+y^2);
f[x_] := U[x, 0, a];
g[x_] := J;
{max1,val1} = Maximize[{U[x,0,a], x < a-1}, x];
{max2,val2} = Maximize[{U[x,0,a], a-1 < x < a}, x];
{max3,val3} = Maximize[{U[x,0,a], x > a}, x];
sol = x /. NSolve[g[x] == f[x] && -1.5 < x < 1.5, x];
Show[
Plot[{f[x], g[x]}, {x, -1.5, 1.5},AxesLabel->{"x","cost. di Jacobi J(x,a)"},
Epilog -> {
{Red, PointSize[0.025],
Point[{x /. val1, max1}],
Point[{x /. val2, max2}],
Point[{x /. val3, max3}],
Text["\!\(\*SubscriptBox[\(L\), \(1\)]\)",{x /. val1, max1-0.4}],
Text["\!\(\*SubscriptBox[\(L\), \(2\)]\)",{x /. val2, max2-0.4}],
Text["\!\(\*SubscriptBox[\(L\), \(3\)]\)",{x /. val3, max3-0.4}]},
{Black, PointSize[0.025],
Point[{a, -6.0}],
Point[{a-1, -6.0}],
Text["Cost. di Jacobi \!\(\*SubscriptBox[\(J\), \(0\)]\)",{1.0, J-0.2}]}
}
],
ListPlot[{#, g[#]} & /# sol, PlotStyle -> PointSize[Large]]
]

Matlab second order derivative

I have followed the tutorial on http://www.mit.edu/people/abbe/matlab/ode.html and prepared a function as follows:
function dxy = diffxy(xy)
%
%split xy into variables in our equations
%
x = xy(1);
xdot = xy(2);
y = xy(3);
%
% define the derivatives of these variables from equations
%
xdot = xdot;
ydot = 3*x + 2*y + 5;
xdoubledot = 3 - ydot + 2*xdot;
%
%return the derivatives in dxy in the right order
%
dxy = [xdot; xdoubledot; ydot]
end
When I call it using
[T, XY] = ode45('diffxy',0,10,[0 1 0])
I get an error
??? Error using ==> diffxy
Too many input arguments.
I also tried
XY= ode45(#diffxy,[0 10],[0;1;0])
Anybody have any idea?
haven't read the whole tutorial but aren't you supposed to define your function as
function dxy = diffxy(t, xy)
where t is time vector