Message Boards Message Boards

GROUPS:

Compare Mathematica with MATLAB by plotting the same equation

Posted 7 months ago
1827 Views
|
6 Replies
|
11 Total Likes
|

Firstly, the MATLAB programme is

t = 0;
A = 2;
c1 = 1;
c2 = 1;
c = 1/25;
omega = 2;

[xi,eta]=meshgrid(-5:0.1:5,-4:0.1:6);

P = ((2*sinh(xi+omega*t))./(3*cosh(xi+omega*t)))+((5*sinh(xi+omega*t))./(6*cosh(xi+omega*t)).^3)+0.9;
Q = -(sinh(eta))./(cosh(eta));
x = xi-2.5*tanh(xi+omega*t);
y = eta;

Px = -(sech(xi+omega*t)).^2;
Qy = -(sech(eta)).^2;

u = ((A-c1.*c2).*Px.*Qy)./(1+c1.*P+c2.*Q+c.*P.*Q).^2;
view([-45 70]);

surf(x,y,u);
xlabel('x','FontSize',26,'FontName','Times New Roman','FontAngle', 'italic')
ylabel('y','FontSize',26,'FontName','Times New Roman','FontAngle', 'italic')
zlabel('$u$','interpreter','latex','FontSize',24,'FontName','Times New Roman','FontAngle', 'italic')

and it comes

enter image description here

With the same equation, the Mathematica program is

\[Omega] = 2;
A = 2; Subscript[c, 1] = Subscript[c, 2] = 1; Subscript[c, 3] = 1/25;
px = -Sech[\[Xi] + \[Omega] t]^2; p = (2 Sinh[\[Xi] + \[Omega] t])/(
  3 Cosh[\[Xi] + \[Omega] t]) + (5 Sinh[\[Xi] + \[Omega] t])/(
  6 Cosh[\[Xi] + \[Omega] t]^3) + 0.9;
qy = -Sech[\[Eta]]^2; q = -(Sinh[\[Eta]]/Cosh[\[Eta]]);
func[\[Xi]_, \[Eta]_, 
   t_] = ((A - Subscript[c, 1] Subscript[c, 2]) px qy)/(1 + 
      Subscript[c, 1] p + Subscript[c, 2] q + Subscript[c, 3] p q)^2 //
     Rationalize // Simplify;
xyToXiEta[x_, y_, t_] := 
  NSolve[{x == \[Xi] - 2.5 Tanh[\[Xi] + 2 t], 
    y == \[Eta]}, {\[Xi], \[Eta]}, Reals];
With[{t = 0}, 
 ListPlot3D[
  Flatten[Table[{x, y, func[\[Xi], \[Eta], t]} /. 
     xyToXiEta[x, y, t], {x, -2, 2, 0.04}, {y, -1, 5, 0.04}], 2], 
  Axes -> True, PlotRange -> {All, All, {0, 2}}, 
  AxesLabel -> {x, y, z}, ColorFunction -> "TemperatureMap"]]

and it comes

enter image description here

Thanks Gianluca Gorni for providing the Mathematica program.

It is obvious that the MATLAB picture forms like a bridge with empty space under it. However, the Mathematica picture is solid.

How can I improve the Mathematica program to make it like MATLAB's result?

Attachments:
POSTED BY: Lingfei Li
6 Replies

A direct translation of the code is not possible as that function does not exist (ListParametricPlot3D or so). But we can recreate the graphic "manually" by recreating the polygons, or rewrite the code to use ParametricPlot3D (preferred and nicer), I included both:

POSTED BY: Sander Huisman
Posted 7 months ago

Thank you for your reply. It is an excellent answer.

POSTED BY: Lingfei Li
Posted 7 months ago

Cross-posted in https://mathematica.stackexchange.com/q/261847/1871

Please include the link(s) of other version(s) of your question when cross-posting to avoid repetive work.

POSTED BY: xzczd  
Posted 7 months ago

Thank you for your notification, I will pay attention to this matter.

POSTED BY: Lingfei Li

It is much easier to bypass x,y and plot directly with respect to xi,eta:

With[{t = 0}, 
 With[{x = \[Xi] - 2.5 Tanh[\[Xi] + 2 t], y = \[Eta]}, 
  ParametricPlot3D[{x, y, func[\[Xi], \[Eta], t]}, {\[Xi], -1, 
    2}, {\[Eta], -1, 4}, PlotRange -> All, 
   ColorFunction -> "TemperatureMap"]]]
POSTED BY: Gianluca Gorni
Posted 7 months ago

Thank you for your reply.

POSTED BY: Lingfei Li
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract