# Matlab 3d linear equation ploting

**URL:** <https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 21, 2022, 12:04am UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564 "2022-07-21T00:04:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [July 21, 2022, 12:04am UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/1 "2022-07-21T00:04:44Z")

</div>

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

```nohighlight
[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);

```

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [July 21, 2022, 12:17am UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/2 "2022-07-21T00:17:15Z")

</div>

This should do the trick!

```julia
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))

```

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [July 21, 2022, 12:55am UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/3 "2022-07-21T00:55:38Z")

</div>

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
julia> f = 2x - 3y .+ 2;

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

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [July 21, 2022, 1:09am UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/4 "2022-07-21T01:09:34Z")

</div>

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
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).

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [July 21, 2022, 1:16am UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/5 "2022-07-21T01:16:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 22, 2022, 3:36pm UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/6 "2022-07-22T15:36:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [July 22, 2022, 4:07pm UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/7 "2022-07-22T16:07:53Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 23, 2022, 8:37am UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/8 "2022-07-23T08:37:47Z")

</div>

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

Which one do you think better PlotlyJS or Makie?

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [July 23, 2022, 12:23pm UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/9 "2022-07-23T12:23:14Z")

</div>

Happy to help! 😄

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.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [July 23, 2022, 3:07pm UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/10 "2022-07-23T15:07:09Z")

</div>

Makie can be used for complex graphs.

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [July 23, 2022, 3:22pm UTC](https://discourse.julialang.org/t/matlab-3d-linear-equation-ploting/84564/11 "2022-07-23T15:22:12Z")

</div>

Check out [this](https://discourse.julialang.org/t/plots-plotlyjs-latex/38250/9) 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!
