Why two plots are not identical Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Plot two graphs in same plot in RSave plot to image file instead of displaying it using Matplotlibselecting certain points to be plottedplot two matrices both of (4*36 double) size in mat labdiscrete derivative of functionUnexpected colors in `surf` plotHow do I plot the electric field of a ring in charge in MATLAB?Matlab: Extract values that I plot but which has not been storedspline interpolation and its (exact) derivativesPlotting arbitrary 3d finite element mesh with matlab

When speaking, how do you change your mind mid-sentence?

Why doesn't the university give past final exams' answers?

Is there a verb for listening stealthily?

Was there ever a LEGO store in Miami International Airport?

Why would the Overseers waste their stock of slaves on the Game?

Are there existing rules/lore for MTG planeswalkers?

Like totally amazing interchangeable sister outfit accessory swapping or whatever

Processing ADC conversion result: DMA vs Processor Registers

Coin Game with infinite paradox

Why is arima in R one time step off?

What helicopter has the most rotor blades?

Marquee sign letters

How to keep bees out of canned beverages?

`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY

Feather, the Redeemed and Dire Fleet Daredevil

"Working on a knee"

Will I lose my paid in full property

When I export an AI 300x60 art board it saves with bigger dimensions

What's called a person who work as someone who puts products on shelves in stores?

What were wait-states, and why was it only an issue for PCs?

Why is water being consumed when my shutoff valve is closed?

Will I be more secure with my own router behind my ISP's router?

What is /etc/mtab in Linux?

Why I cannot instantiate a class whose constructor is private in a friend class?



Why two plots are not identical



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Plot two graphs in same plot in RSave plot to image file instead of displaying it using Matplotlibselecting certain points to be plottedplot two matrices both of (4*36 double) size in mat labdiscrete derivative of functionUnexpected colors in `surf` plotHow do I plot the electric field of a ring in charge in MATLAB?Matlab: Extract values that I plot but which has not been storedspline interpolation and its (exact) derivativesPlotting arbitrary 3d finite element mesh with matlab



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to plot functions in MATLAB. In the first plot I write a function and use the gradient command to plot the function. In second plot I have taken the derivative of a function and then plotted it. But the two plots are not identical. Where is the discrepancy?



MATLAB Code:



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=m.*sin(x+4.*t);
dr1=gradient(r1);
dt=gradient(t);
dr1dt=dr1./min(diff(T));
td=t(2:end);

surf(x,t,abs(dr1dt));


I have plotted it.



plot of the first code



Then I take the derivative of r1 w.r.t t and then plot the function. i.e.,



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=4.*m.*cos(x+4.*t);

surf(x,t,abs(r1)');


plot of the secondcode



I have found that there is difference of amplitude in these plots. Why are the two plots not identical?










share|improve this question
























  • You didn't take the derivative correctly

    – Ben Voigt
    Mar 9 at 5:18











  • @ Ben, actually there was $cos$ instead of $sin$. Its a typo mistake

    – A. Riaz
    Mar 9 at 5:32











  • Ordinarily you can fix mistakes like that by using the "edit" link under your question. But right now another user's edits are pending, so it may not allow you to make changes until those are accepted or rejected.

    – Ben Voigt
    Mar 9 at 5:34











  • Are you asking about the difference between the derivative of a continuous function and an approximation to the derivative determined using a discretized version of that function? Or are the differences really important here? Maybe if you upload the two plots it’s be easier to see what kind of differences we’re talking about here.

    – Cris Luengo
    Mar 9 at 17:05











  • @ Luengo, I have uploaded the plots.

    – A. Riaz
    Mar 11 at 1:20

















0















I am trying to plot functions in MATLAB. In the first plot I write a function and use the gradient command to plot the function. In second plot I have taken the derivative of a function and then plotted it. But the two plots are not identical. Where is the discrepancy?



MATLAB Code:



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=m.*sin(x+4.*t);
dr1=gradient(r1);
dt=gradient(t);
dr1dt=dr1./min(diff(T));
td=t(2:end);

surf(x,t,abs(dr1dt));


I have plotted it.



plot of the first code



Then I take the derivative of r1 w.r.t t and then plot the function. i.e.,



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=4.*m.*cos(x+4.*t);

surf(x,t,abs(r1)');


plot of the secondcode



I have found that there is difference of amplitude in these plots. Why are the two plots not identical?










share|improve this question
























  • You didn't take the derivative correctly

    – Ben Voigt
    Mar 9 at 5:18











  • @ Ben, actually there was $cos$ instead of $sin$. Its a typo mistake

    – A. Riaz
    Mar 9 at 5:32











  • Ordinarily you can fix mistakes like that by using the "edit" link under your question. But right now another user's edits are pending, so it may not allow you to make changes until those are accepted or rejected.

    – Ben Voigt
    Mar 9 at 5:34











  • Are you asking about the difference between the derivative of a continuous function and an approximation to the derivative determined using a discretized version of that function? Or are the differences really important here? Maybe if you upload the two plots it’s be easier to see what kind of differences we’re talking about here.

    – Cris Luengo
    Mar 9 at 17:05











  • @ Luengo, I have uploaded the plots.

    – A. Riaz
    Mar 11 at 1:20













0












0








0








I am trying to plot functions in MATLAB. In the first plot I write a function and use the gradient command to plot the function. In second plot I have taken the derivative of a function and then plotted it. But the two plots are not identical. Where is the discrepancy?



MATLAB Code:



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=m.*sin(x+4.*t);
dr1=gradient(r1);
dt=gradient(t);
dr1dt=dr1./min(diff(T));
td=t(2:end);

surf(x,t,abs(dr1dt));


I have plotted it.



plot of the first code



Then I take the derivative of r1 w.r.t t and then plot the function. i.e.,



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=4.*m.*cos(x+4.*t);

surf(x,t,abs(r1)');


plot of the secondcode



I have found that there is difference of amplitude in these plots. Why are the two plots not identical?










share|improve this question
















I am trying to plot functions in MATLAB. In the first plot I write a function and use the gradient command to plot the function. In second plot I have taken the derivative of a function and then plotted it. But the two plots are not identical. Where is the discrepancy?



MATLAB Code:



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=m.*sin(x+4.*t);
dr1=gradient(r1);
dt=gradient(t);
dr1dt=dr1./min(diff(T));
td=t(2:end);

surf(x,t,abs(dr1dt));


I have plotted it.



plot of the first code



Then I take the derivative of r1 w.r.t t and then plot the function. i.e.,



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=4.*m.*cos(x+4.*t);

surf(x,t,abs(r1)');


plot of the secondcode



I have found that there is difference of amplitude in these plots. Why are the two plots not identical?







matlab plot derivative






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 11 at 6:02









Cris Luengo

23.5k52254




23.5k52254










asked Mar 9 at 4:20









A. RiazA. Riaz

33




33












  • You didn't take the derivative correctly

    – Ben Voigt
    Mar 9 at 5:18











  • @ Ben, actually there was $cos$ instead of $sin$. Its a typo mistake

    – A. Riaz
    Mar 9 at 5:32











  • Ordinarily you can fix mistakes like that by using the "edit" link under your question. But right now another user's edits are pending, so it may not allow you to make changes until those are accepted or rejected.

    – Ben Voigt
    Mar 9 at 5:34











  • Are you asking about the difference between the derivative of a continuous function and an approximation to the derivative determined using a discretized version of that function? Or are the differences really important here? Maybe if you upload the two plots it’s be easier to see what kind of differences we’re talking about here.

    – Cris Luengo
    Mar 9 at 17:05











  • @ Luengo, I have uploaded the plots.

    – A. Riaz
    Mar 11 at 1:20

















  • You didn't take the derivative correctly

    – Ben Voigt
    Mar 9 at 5:18











  • @ Ben, actually there was $cos$ instead of $sin$. Its a typo mistake

    – A. Riaz
    Mar 9 at 5:32











  • Ordinarily you can fix mistakes like that by using the "edit" link under your question. But right now another user's edits are pending, so it may not allow you to make changes until those are accepted or rejected.

    – Ben Voigt
    Mar 9 at 5:34











  • Are you asking about the difference between the derivative of a continuous function and an approximation to the derivative determined using a discretized version of that function? Or are the differences really important here? Maybe if you upload the two plots it’s be easier to see what kind of differences we’re talking about here.

    – Cris Luengo
    Mar 9 at 17:05











  • @ Luengo, I have uploaded the plots.

    – A. Riaz
    Mar 11 at 1:20
















You didn't take the derivative correctly

– Ben Voigt
Mar 9 at 5:18





You didn't take the derivative correctly

– Ben Voigt
Mar 9 at 5:18













@ Ben, actually there was $cos$ instead of $sin$. Its a typo mistake

– A. Riaz
Mar 9 at 5:32





@ Ben, actually there was $cos$ instead of $sin$. Its a typo mistake

– A. Riaz
Mar 9 at 5:32













Ordinarily you can fix mistakes like that by using the "edit" link under your question. But right now another user's edits are pending, so it may not allow you to make changes until those are accepted or rejected.

– Ben Voigt
Mar 9 at 5:34





Ordinarily you can fix mistakes like that by using the "edit" link under your question. But right now another user's edits are pending, so it may not allow you to make changes until those are accepted or rejected.

– Ben Voigt
Mar 9 at 5:34













Are you asking about the difference between the derivative of a continuous function and an approximation to the derivative determined using a discretized version of that function? Or are the differences really important here? Maybe if you upload the two plots it’s be easier to see what kind of differences we’re talking about here.

– Cris Luengo
Mar 9 at 17:05





Are you asking about the difference between the derivative of a continuous function and an approximation to the derivative determined using a discretized version of that function? Or are the differences really important here? Maybe if you upload the two plots it’s be easier to see what kind of differences we’re talking about here.

– Cris Luengo
Mar 9 at 17:05













@ Luengo, I have uploaded the plots.

– A. Riaz
Mar 11 at 1:20





@ Luengo, I have uploaded the plots.

– A. Riaz
Mar 11 at 1:20












1 Answer
1






active

oldest

votes


















0














The function gradient, with one output argument, computes the partial derivative in the x direction. Thus, what you compute in the first bit of code is d/dx r1, not d/dt r1. With two output arguments it computes both the x and the y derivatives.



In the second bit of code, the computed derivative is transposed, leading to swapping the x and t axes.



The following bit of code fixes both these errors:



X=-1:.05:1;
T=-1:.05:1;
m=1+1*1i;
[x,t]=meshgrid(X,T);
r1=m.*sin(x+4.*t);

[dr1dx,dr1dt]=gradient(r1);
dr1dx=dr1dx/mean(diff(X));
dr1dt=dr1dt/mean(diff(T));

dr1dt_true=4.*m.*cos(x+4.*t);

figure
subplot(1,2,1)
surf(x,t,abs(dr1dt));
xlabel('x')
ylabel('t')
title('discrete approximation')
subplot(1,2,2)
surf(x,t,abs(dr1dt_true));
xlabel('x')
ylabel('t')
title('analytical')


output of code above






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073949%2fwhy-two-plots-are-not-identical%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The function gradient, with one output argument, computes the partial derivative in the x direction. Thus, what you compute in the first bit of code is d/dx r1, not d/dt r1. With two output arguments it computes both the x and the y derivatives.



    In the second bit of code, the computed derivative is transposed, leading to swapping the x and t axes.



    The following bit of code fixes both these errors:



    X=-1:.05:1;
    T=-1:.05:1;
    m=1+1*1i;
    [x,t]=meshgrid(X,T);
    r1=m.*sin(x+4.*t);

    [dr1dx,dr1dt]=gradient(r1);
    dr1dx=dr1dx/mean(diff(X));
    dr1dt=dr1dt/mean(diff(T));

    dr1dt_true=4.*m.*cos(x+4.*t);

    figure
    subplot(1,2,1)
    surf(x,t,abs(dr1dt));
    xlabel('x')
    ylabel('t')
    title('discrete approximation')
    subplot(1,2,2)
    surf(x,t,abs(dr1dt_true));
    xlabel('x')
    ylabel('t')
    title('analytical')


    output of code above






    share|improve this answer



























      0














      The function gradient, with one output argument, computes the partial derivative in the x direction. Thus, what you compute in the first bit of code is d/dx r1, not d/dt r1. With two output arguments it computes both the x and the y derivatives.



      In the second bit of code, the computed derivative is transposed, leading to swapping the x and t axes.



      The following bit of code fixes both these errors:



      X=-1:.05:1;
      T=-1:.05:1;
      m=1+1*1i;
      [x,t]=meshgrid(X,T);
      r1=m.*sin(x+4.*t);

      [dr1dx,dr1dt]=gradient(r1);
      dr1dx=dr1dx/mean(diff(X));
      dr1dt=dr1dt/mean(diff(T));

      dr1dt_true=4.*m.*cos(x+4.*t);

      figure
      subplot(1,2,1)
      surf(x,t,abs(dr1dt));
      xlabel('x')
      ylabel('t')
      title('discrete approximation')
      subplot(1,2,2)
      surf(x,t,abs(dr1dt_true));
      xlabel('x')
      ylabel('t')
      title('analytical')


      output of code above






      share|improve this answer

























        0












        0








        0







        The function gradient, with one output argument, computes the partial derivative in the x direction. Thus, what you compute in the first bit of code is d/dx r1, not d/dt r1. With two output arguments it computes both the x and the y derivatives.



        In the second bit of code, the computed derivative is transposed, leading to swapping the x and t axes.



        The following bit of code fixes both these errors:



        X=-1:.05:1;
        T=-1:.05:1;
        m=1+1*1i;
        [x,t]=meshgrid(X,T);
        r1=m.*sin(x+4.*t);

        [dr1dx,dr1dt]=gradient(r1);
        dr1dx=dr1dx/mean(diff(X));
        dr1dt=dr1dt/mean(diff(T));

        dr1dt_true=4.*m.*cos(x+4.*t);

        figure
        subplot(1,2,1)
        surf(x,t,abs(dr1dt));
        xlabel('x')
        ylabel('t')
        title('discrete approximation')
        subplot(1,2,2)
        surf(x,t,abs(dr1dt_true));
        xlabel('x')
        ylabel('t')
        title('analytical')


        output of code above






        share|improve this answer













        The function gradient, with one output argument, computes the partial derivative in the x direction. Thus, what you compute in the first bit of code is d/dx r1, not d/dt r1. With two output arguments it computes both the x and the y derivatives.



        In the second bit of code, the computed derivative is transposed, leading to swapping the x and t axes.



        The following bit of code fixes both these errors:



        X=-1:.05:1;
        T=-1:.05:1;
        m=1+1*1i;
        [x,t]=meshgrid(X,T);
        r1=m.*sin(x+4.*t);

        [dr1dx,dr1dt]=gradient(r1);
        dr1dx=dr1dx/mean(diff(X));
        dr1dt=dr1dt/mean(diff(T));

        dr1dt_true=4.*m.*cos(x+4.*t);

        figure
        subplot(1,2,1)
        surf(x,t,abs(dr1dt));
        xlabel('x')
        ylabel('t')
        title('discrete approximation')
        subplot(1,2,2)
        surf(x,t,abs(dr1dt_true));
        xlabel('x')
        ylabel('t')
        title('analytical')


        output of code above







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 11 at 6:07









        Cris LuengoCris Luengo

        23.5k52254




        23.5k52254





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073949%2fwhy-two-plots-are-not-identical%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved