How can I plot a function for pringle shape - plot

I would like to reproduce pringle shape in Octave.
Please, look at the attachment - that is done in Mathematica using "Region" function.
Is possible to cut that circular shape also in Octave? Until now I've got this (code below) but it has rectangular shape/ platform not circular like in Mathematica (picture below).
a = 10
b = 10
x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2/a.^2 - yy.^2/b.^2
h = surf(xx,yy,z);
colormap hsv;
set(h,'linestyle','none');

Related

Drawing an equiangular spiral between two known points in Julia

I have two random points in a 2D Cartesian grid, p1 and p2. I would like to define a curve between p1 and p2 of N points such that the curve forms an equiangular spiral (similar to what is done in this paper (Fig. 8)). I've tried converting the paper into a script, but something is still off, so I'm trying to build a "dumbed down" example. My closest attempt is this (p2 can be seen on zoom-in, but not shown in script's plot):
using PyPlot
using LinearAlgebra
p1 = [5,7]
p2 = [1,2]
r = norm(p1-p2)
theta_offset = tan((p1[2]-p2[2])/(p1[1]-p2[1]));
# Number of points
rez = 500
# Number of revolutions
rev = 5
# Radius as spiral decreases
t = range(0,r,rez)
# Angle as spiral decreases
theta = range(0, 2*pi*rev, rez) .+ theta_offset
x = cos.(theta).*exp.(-t).+p2[1];
y = sin.(theta).*exp.(-t).+p2[2];
figure()
plot(x,y)
scatter(p1[1],p1[2],c="red",s=5)
scatter(p2[1],p2[2],c="red",s=10)
show(); gcf()
which produces the following plot:
While the plot is centered on p2 (at coordinate [1,2]), the endpoint does not lie near / pass through my specified point p1. My ideal outcome would be something like this:
EDIT: Solved problem using #PaSTE's suggestion. Changing my theta_offset, x, and y coordinate calculations to:
theta_offset = atan((p1[2]-p2[2])/(p1[1]-p2[1]));
x = cos.(theta).*exp.(-t).*r.+p2[1]
y = sin.(theta).*exp.(-t).*r.+p2[2]
yields the following plot, exactly what I was hoping for. In my solution, handedness and number of loops are not important.
As PaSTE said, The issue is the algebra: you need get the angle with atan, not tan, and multiply your exponential factor by r to get the right radius.
using LinearAlgebra
using Plots
p1 = [5,7]
p2 = [1,2]
r = norm(p1-p2)
theta_offset = atan((p1[2]-p2[2])/(p1[1]-p2[1]));
# Number of points
rez = 1500
# Number of revolutions
rev = 5
# Radius as spiral decreases
t = range(0,r,rez)
# Angle as spiral decreases
theta = range(0, 2*pi*rev, rez) .+ theta_offset
x = cos.(theta) .* r .* exp.(-t) .+ p2[1]
y = sin.(theta) .* r .* exp.(-t) .+ p2[2]
plot(x, y)
scatter!([p1[1], p2[1]], [p1[2], p2[2]])

Contours with equal area changing system in julia

Let's say I have a system with 3 particles in 2d system a,b,c with the following positions
a = [0.1,0.0]
b = [-0.1,0.0]
c = [0.0,0.1]
each particle is described by Gaussian function, therefore the system function is as followed:
σ = 0.08
f(x,y) = exp(-((x-a[1])^2+(y-a[2])^2)/(2(σ^2)))+exp(-((x-b[1])^2+(y-b[2])^2)/(2(σ^2)))+exp(-((x-c[1])^2+(y-c[2])^2)/(2(σ^2)))
I draw the initial system shape
x = range(-0.5,stop=0.5,length=50)
y = range(-0.5,stop=0.5,length=50)
contour(x,y,f,levels=[1.0],aspect_ratio=:equal)
using Plots package. After a while the particles move to other positions,for instance:
a = [0.1,-0.05]
b = [-0.1,0.0]
c = [0.05,0.1]
which changes the shape of the contour according to the same f(x,y) function (with the same sigma).
I need to produce a closed contour that keep the same area for the new particle positions.
How do I do that? or How can I find the "level" to reach the same area?
This was an example, in fact I have many particles in my function.

How to convert this equation to Octave code and plot

I have an equation that I created on Desmos website
I used the code below to try and recreate it in Octave. But when I plot it, it comes out different. How can I fix the code in Octave (without changing the main equation, if possible) so it looks like the Desmos image?
x = linspace(0,1,20);
y = linspace(0,1,20);
S=[13.2];
T=1.12;
for zz=1:1:length(S)
eq1=exp(S(1,zz)*T*log(x))+exp(S(1,zz)*T*log(y));
hold on
plot(x,eq1)
zz
end
PS: I'm using Octave 4.2.2
S = 13.2;
T = 1.12;
f = #(x)exp(log(1-exp(S*T*log(x)))./(S*T));
fplot(f, [0, 1])
Desmos.com does not plot (x,eq1) but (x,y) with the constraint that x, y satisfy the given equation. So, you solve for y for each value of x, and plot the pairs (x,y).
Since log(x), log(y) exist, x and y are >0 (otherwise you would have to plot for x<0 as well).
clear; clc;
x = linspace(0,1,150);
S = 13.2;
T = 1.12;
y = zeros(size(x));
for i = 1:length(x)
y(i) = (1-exp(S*T*log(x(i))))^(1/S/T);
end
plot(x,y)
Notes:
1) I assume by log(x) you mean ln(x) (logarithm using base e).
2) I used a more dense discretization with 150 points so that the plotted curve appears smoother.
3) Mathematically, linspace(0,1,150) should not work, as log(x=0) is not defined. However for Matlab log(0) = -inf which means that exp(-inf) = 0. That's why no runtime error is thrown.
4) By the way, the provided equation can be simplified to x^(ST) + y^(ST) = 1, with the constraints that x, y > 0.

Creating a hexplot

I am trying to create a figure like the one depicted in the third column of the following image:
Link for the image in case of backup.
Basically I have x and y positions of 200 particles and I have the MSD data for these 200 positions. I'd like MSD to be the value that should determine a color map for the particles in coordinates (x,y). So MSD should be like the height, or the z position corresponding to each particle in (x,y).
I am surprised at my incompetence, because I have been trying to solve this problem for the last couple of days but none of the Google searches gave me any result. The closest thing that I have found is the concept of "self-organizing map" in Matlab and R, but I do not know how to use R and Matlab's toolbox for SOM was utterly useful for my needs.
I tried the following code in Matlab and get the attached plot as a result:
clear all; close all; clc;
x = (dlmread('xdata.dat'))'; % x is 1x200 array
y = (dlmread('ydata.dat'))'; % y is 1x200 array
msd = (dlmread('msd_field.txt'))'; % msd is 1x200 array
[X,Y] = meshgrid(x,y);
Z = meshgrid(msd);
z = [X; Y; Z];
surf(z)
But I think this plot is not useful at all. What I want is a 2D scatter plot of (x,y) depicting particle positions and on top of that color code this scatter plot with the values stored in msd like the plot I showed in the beginning. How can I create this through Matlab, or any other visualization tool? Thank you in advance.
It is not clear whay you want to have. Here a scatter plot using ggplot2.
## some reproducible data
set.seed(1)
dat <- data.frame(
x = round(runif(200,-30,30),2),
y = round(runif(200,-2,30),2),
msd = sample(c(0,2,3),200,rep=T))
## scatter plot where the size/color of points depends in msd
library(ggplot2)
ggplot(dat) +
geom_point(aes(x,y,size=msd,color=msd)) +
theme_bw()

Color a 3d surface created using curve3d (R package:emdbook) according to value of z

I'm plotting a 3D surface in R using the curve3d function from the emdbook package. An example:
curve3d(expr=219.4*exp(-0.5*(((x--1.3)/0.6)^2+((y-2.4)/0.8)^2))+
80.1*exp(-0.5*(((x-0.1)/0.7)^2+((y--1.3)/0.4)^2))+
52.1*exp(-0.5*(((x-0.2)/0.5)^2+((y-0.8)/0.5)^2)),
from = c(-3,-3), to = c(3,5), n = 50, sys3d = "persp",
theta=145,phi=30,zlab="z")
Now, I'd like to colour the surface according to the z value. I've looked at the example for the persp function where the surface is coloured according to z, but that's not helpful because I'm not defining z myself when using curve3d. Is there a way to do this using curve3d?

Resources