× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Incorrect graph when trying to plot z = x + y with MATLAB

Ray Phan
Jul 09, 2015
<p>Your syntax for generating the 3D coordinates is right. Your call to <code>surf</code> is incorrect. What you actually need to do is separate <code>x</code>, <code>y</code> and <code>z</code> into three separate parameters:</p> <pre><code>surf(x,y,z); </code></pre> <p>When you do that, you get this surface. Take note that the figure generated was using MATLAB R2013a, so the colour map shown is not the parula colour map that is available as of R2014b and up, but the surface will be the right one that is what you're looking for:</p> <p><img src="http://i.stack.imgur.com/7y5u8.png" alt="enter image description here"></p> <p>... now why do you need to separate your <code>x</code>, <code>y</code> and <code>z</code> points to create the surface? The reason why is because doing <code>[x,y,z]</code> means that you are <strong>concatenating</strong> the <code>x</code>, <code>y</code> and <code>z</code> coordinates into a <strong>single 2D signal</strong>, and so what's happening is that you are creating a 2D signal that is 10 x 30. Calling <code>surf</code> with this single 2D array automatically assumes that the <code>x</code> values span from 1 to 30 and the <code>y</code> values span from 1 to 10 and those are the 2D grid of values that span the axis of your <code>surf</code> plot in conjunction with the <code>z</code> values shown, where the <code>z</code> values originate from the concatenated matrix created earlier. If you look at the plot you generated, you can see the <code>x</code> values are spanning from 1 to 30, and that's obviously not what you want.</p> <p>You need to separate the <code>x</code>, <code>y</code> and <code>z</code> values to achieve the desired plane.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31227056/Incorrect%20graph%20when%20trying%20to%20plot%20z%20=%20x%20+%20y%20with%20MATLAB/31227127">Stack Overflow</a>.</p>
comments powered by Disqus