I have been looking in the forum and within the graphical functions of the program help, how to graph the following function:
x2=[-2:0.02:2]';x1=[-1:0.01:1]';
function val = Heart(x1, x2)
val=(1.2*x2-sqrt(abs(x1)))^2+x1^2-1;
endfunction
And I have not found anything to guide me.
I try to reproduce this maple plot:
Heart curve
an other solution is to solve the equation (1.2*x2-sqrt(abs(x1)))^2+x1^2-1 = c
for x2
The solution is quite easy
and one can found 2 branches
x2=(5/6)(sqrt(abs(x1))+sqrt(c+1-x1^2));
and
x2=(5/6)(sqrt(abs(x1))-sqrt(c+1-x1^2));
c=0;
x1=linspace(-1,1,200);
x1r=x1($:-1:1);
x2=(5/6)*[ (sqrt(abs(x1))+sqrt(c+1-x1^2)) (sqrt(abs(x1r))-sqrt(c+1- x1r^2))];
clf;plot([x1 x1r],x2);
Since your defining a 3D surface, you can use contour and contour2d: as said by luispauloml, you can pass the function directly as a parameter.
x1=[-1:0.01:1]
x2=[-2:0.02:2]
function val = Heart(x1, x2)
val=(1.2*x2-sqrt(abs(x1))).^2+x1.^2-1; // switched ^ to .^ to handle vectors
endfunction;
figure()
xlabel('x1')
ylabel('x2')
contour2d(x1,x2,Heart,[0 0]);
a=gca()
hline=a.children.children(1)
hline.foreground=color('red')
hline.thickness=2
Related
I am using Plots for visualizing 3d-plots with Julia, and I am trying to change camera angle of my plot. In matplotlib in Python, I know that I can use ax.view_init(elev, azim) to change the camera angle, but on Plot.jl, I could not find solution to change the angle.
Is there any equivalent function with ax.view_init(elev, azim) in Python in Julia ?
Example of Plot
using Plots
plot()
for i in 1 : 5
a = rand(10); b= rand(10); c = rand(10);
plot!(a,b,c, seriestype=:scatter)
end
plot!()
As you can read in the manual you can use the camera keyword argument (aliases are: cam, cameras, view_angle, viewangle). This argument sets the view angle for 3D plots. Its value is required to be a tuple (azimuthal, elevation) and the default setting is (30, 30).
This should hopefully not be a very hard question—I'm just not very experienced in R.
If I want to graph a simple sine wave, all I have to is:
x=seq(-20,20,0.001)
y=seq(-20,20,0.001)
y=sin(x)
plot(x,y,type="l")
But let's say I want to graph a relationship with trigonometric functions on both sides, such as sin(x) = cos(y). Typing:
sin(x) = cos(y)
Gives me the following error
Error in sin(x) = cos(y) : could not find function "sin<-"
Now, the obvious solution is to just rearrange it in terms of one variable, such as x = asin(cos(y)). But with much more complicated equations with multiple nested trigonometric functions on both sides, this no longer becomes viable.
I'm sure I'm missing something extremely obvious, but what is it?
If you want to plot the relationship sin on the x axis and cos on the y axis:
plot(sin(x), cos(y), type = "l")
Or did I misunderstand the question?
The error is caused by your equal sign. The expression sin(x) = cos(y) is an assignment. If you would like to check where they are equal, then you should write sin(x) == cos(x).
I am new to Julia, I would like to solve this system:
where k1 and k2 are constant parameters. However, I=0 when y,0 or Ky otherwise, where k is a constant value.
I followed the tutorial about ODE. The question is, how to solve this piecewise differential equation in DifferentialEquations.jl?
Answered on the OP's cross post on Julia Discourse; copied here for completeness.
Here is a (mildly) interesting example $x''+x'+x=\pm p_1$ where the sign of $p_1$ changes when a switching manifold is encountered at $x=p_2$. To make things more interesting, consider hysteresis in the switching manifold such that $p_2\mapsto -p_2$ whenever the switching manifold is crossed.
The code is relatively straightforward; the StaticArrays/SVector/MVector can be ignored, they are only for speed.
using OrdinaryDiffEq
using StaticArrays
f(x, p, t) = SVector(x[2], -x[2]-x[1]+p[1]) # x'' + x' + x = ±p₁
h(u, t, integrator) = u[1]-integrator.p[2] # switching surface x = ±p₂;
g(integrator) = (integrator.p .= -integrator.p) # impact map (p₁, p₂) = -(p₁, p₂)
prob = ODEProblem(f, # RHS
SVector(0.0, 1.0), # initial value
(0.0, 100.0), # time interval
MVector(1.0, 1.0)) # parameters
cb = ContinuousCallback(h, g)
sol = solve(prob, Vern6(), callback=cb, dtmax=0.1)
Then plot sol[2,:] against sol[1,:] to see the phase plane - a nice non-smooth limit cycle in this case.
Note that if you try to use interpolation of the resulting solution (i.e., sol(t)) you need to be very careful around the points that have a discontinuous derivative as the interpolant goes a little awry. That's why I've used dtmax=0.1 to get a smoother solution output in this case. (I'm probably not using the most appropriate integrator either but it's the one that I was using in a previous piece of code that I copied-and-pasted.)
I'm trying to plot the following implicit formula in R:
1 = x^2 + 4*(y^2) + x*y
which should be an ellipse. I'd like to randomly sample the x values and then generate the graph based on those.
Here's a related thread, but the solutions there seem to be specific to the 3D case. This question has been more resistant to Googling that I would have expected, so maybe the R language calls implicit formulas something else.
Thanks in advance!
Two things you may not understand. When plotting implicit functions with that technique, you need to move all terms to the RHS of the function so that your implicit function becomes:
0 = -1+ x^2 + 4*(y^2) + x*y
Then using the contour value of zero will make sense:
x<-seq(-1.1,1.1,length=1000)
y<-seq(-1,1,length=1000)
z<-outer(x,y,function(x,y) 4*y^2+x^2+x*y -1 )
contour(x,y,z,levels=0)
I got a sign wrong on the first version. #mnels' was correct.
Let us consider the following scenario .
for x in range (1,100)
for y in range (2,500)
#plot(f(x),g(y))
end
end
where f(x) and g(y) are some user defined functions.
The output must be the desired points on plane.
Is there any way in julia to do like what I need ?
In general I can do like this
for x in range (1,100)
for y in range (2,500)
push!(l,f(x))
push!(m,g(y))
end
end
and then plotting from the two lists l,m as x,y axes respectively.
But now I want to plot points while executing loop.
This is mostly supported in Plots... see https://github.com/tbreloff/Plots.jl/issues/30 for a little more information and some example usage.
use the display function:
for x in 1:100
p = plot(f(x),g(y))
display(p)
sleep(1)
end
(inspired by Andreas Peter on the Julia slack #helpdesk channel)