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;
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.
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)');
I have found that there is difference of amplitude in these plots. Why are the two plots not identical?
matlab plot derivative
add a comment |
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.
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)');
I have found that there is difference of amplitude in these plots. Why are the two plots not identical?
matlab plot derivative
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
add a comment |
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.
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)');
I have found that there is difference of amplitude in these plots. Why are the two plots not identical?
matlab plot derivative
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.
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)');
I have found that there is difference of amplitude in these plots. Why are the two plots not identical?
matlab plot derivative
matlab plot derivative
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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')
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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')
add a comment |
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')
add a comment |
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')
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')
answered Mar 11 at 6:07
Cris LuengoCris Luengo
23.5k52254
23.5k52254
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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