Matlab 3d linear equation ploting

Hello, good evening.
How I can port this matlab code for julia?

[x, y] = meshgrid(-5:0.5:5); %define o número de passos no intervalo [-5,5]
z = 2*x - 3*y + 2;
surf(x, y, z);

This should do the trick!

using Plots

x = collect(-5:0.5:5)
y = collect(-5:0.5:5)

z(x, y) = 2*x - 3*y + 2

plot(x, y, z, st = :surface, camera = (10, 10))

Thats insane. My only issue was not use collect in range and did vectorized operation. All the plot got wrong. The julia error messages are wierd.

julia> f = 2x - 3y .+ 2;

julia> surface(x, y, f)
 ***   ALL COLLINEAR DATA POINTS.
   NDP =  101
 ERROR DETECTED IN ROUTINE   IDTANG.

That is not a julia error but a GR error message which I agree is very weird. GR is the C library that I Plots.jl uses by default.

The problem is not about collect (if you do it without it it still works).

The problem is that in your attempt, f = 2x - 3y .+ 2 is building a vector, not a matrix as you thought. In @liamfdoherty’s answer, their z(x,y) is a a function which Plots knows how to apply. Another option would have been:

julia> f = 2x .- 2y' .+ 2;

julia> f
21×21 Matrix{Float64}:
  2.0   1.0   0.0  -1.0  -2.0  -3.0  …  -14.0  -15.0  -16.0  -17.0  -18.0
  3.0   2.0   1.0   0.0  -1.0  -2.0     -13.0  -14.0  -15.0  -16.0  -17.0
  4.0   3.0   2.0   1.0   0.0  -1.0     -12.0  -13.0  -14.0  -15.0  -16.0
  5.0   4.0   3.0   2.0   1.0   0.0     -11.0  -12.0  -13.0  -14.0  -15.0
  6.0   5.0   4.0   3.0   2.0   1.0     -10.0  -11.0  -12.0  -13.0  -14.0
  7.0   6.0   5.0   4.0   3.0   2.0  …   -9.0  -10.0  -11.0  -12.0  -13.0
  8.0   7.0   6.0   5.0   4.0   3.0      -8.0   -9.0  -10.0  -11.0  -12.0
  9.0   8.0   7.0   6.0   5.0   4.0      -7.0   -8.0   -9.0  -10.0  -11.0
 10.0   9.0   8.0   7.0   6.0   5.0      -6.0   -7.0   -8.0   -9.0  -10.0
 11.0  10.0   9.0   8.0   7.0   6.0      -5.0   -6.0   -7.0   -8.0   -9.0
 12.0  11.0  10.0   9.0   8.0   7.0  …   -4.0   -5.0   -6.0   -7.0   -8.0
 13.0  12.0  11.0  10.0   9.0   8.0      -3.0   -4.0   -5.0   -6.0   -7.0
 14.0  13.0  12.0  11.0  10.0   9.0      -2.0   -3.0   -4.0   -5.0   -6.0
 15.0  14.0  13.0  12.0  11.0  10.0      -1.0   -2.0   -3.0   -4.0   -5.0
 16.0  15.0  14.0  13.0  12.0  11.0       0.0   -1.0   -2.0   -3.0   -4.0
 17.0  16.0  15.0  14.0  13.0  12.0  …    1.0    0.0   -1.0   -2.0   -3.0
 18.0  17.0  16.0  15.0  14.0  13.0       2.0    1.0    0.0   -1.0   -2.0
 19.0  18.0  17.0  16.0  15.0  14.0       3.0    2.0    1.0    0.0   -1.0
 20.0  19.0  18.0  17.0  16.0  15.0       4.0    3.0    2.0    1.0    0.0
 21.0  20.0  19.0  18.0  17.0  16.0       5.0    4.0    3.0    2.0    1.0
 22.0  21.0  20.0  19.0  18.0  17.0  …    6.0    5.0    4.0    3.0    2.0

julia> surface(x,y,f)

Which may be closer to what you expected (in this case f is a matrix).

1 Like

Understood. In octave all is a matrix. There are no distinction between a row matrix or a column matrix.

How to make it interactive / we can view it at different angle by dragging the image. Perhaps using Makie? or PlotlyJS? Anyone ever tried for 3d plotting like this?

Sure, using PlotlyJS is as easy as adding it with using Pkg; Pkg.add("PlotlyJS") and then making the minor modification

using Plots; plotlyjs()

x = collect(-5:0.5:5)
y = collect(-5:0.5:5)

z(x, y) = 2*x - 3*y + 2

plot(x, y, z, st = :surface, camera = (10, 10))

(i.e., changing the backend for Plots in line 1)! This will generate a window with click-and-drag functionality.

1 Like

Can I add this as another solution? haha thanks a lot!

Which one do you think better PlotlyJS or Makie?

Happy to help! :smile:

I have not had very much experience with Makie, but just from the documentation page it seems to be more powerful than any of the standard backends for Plots.jl. That said, the more complex things you want to do, the more code it will take (of course)! As with anything, which is “better” probably depends on the use case.

Makie can be used for complex graphs.

Anyway, I try to add something, is using Plots and PlotlyJS can’t make us use LaTeXStrings?

using Plots, LaTeXStrings, Plots.PlotMeasures 
plotlyjs()

x = collect(-5:0.5:5)
y = collect(-5:0.5:5)

z(x, y) = x^2 - 3*y + 2

l = L"x^2 - 3y + 2"

plot(x, y, z, st = :surface, camera = (10, 10))
title!(l)

it showing the raw strings instead of the right mathematical notations.

1 Like

Check out this thread that addresses the issue. It appears that LaTeX can only be properly rendered through MathJax when saved as an HTML file, but if you are able to do that, this should give you what you need!

1 Like