plotting a derivative of a function using surf command

2 次查看(过去 30 天)
I am trying to plot a derivative of a function using surf command, but when I evaluate it an error occur "Data dimensions must agree".
This is what I typed:
close all;
X=-10:.1:10;
T=-10:.1:10;
mu1=-.01+1*1i;
a=(.1-2*1i);
b=(.1-.1*1i);
[x,t]=meshgrid(X,T);
x1=exp(-1i*mu1.*x).*exp((1-(1i./(2*mu1)).*t));
x2=exp(1i*mu1.*x).*exp((1+(1i./(2*mu1)).*t));
y1=exp(-1i*mu1.*x).*exp((1+(1i./(2*mu1)).*t));
y2=exp(1i*mu1.*x).*exp((1-(1i./(2*mu1)).*t));
A=2*1i*(1)*a*conj(b)*x1.*conj(x2).*(x2.*conj(y1)-y2.*conj(x1));
B=(a.*conj(a).*x1.*conj(x1).*y2.*conj(y2)+b.*conj(b).*x1.*conj(y1).*x2.*conj(y2)+a.*conj(a).*x2.*conj(x2).*y1.*conj(y1)+b.*conj(b).*y1.*conj(x2).*y2.*conj(x1));
r1=-2.*1i.*((A./B));
dr1=diff(r1);
dt=diff(t);
dr1dt=dr1./dt;
td=t(2:end);
surf(x,td,abs(dr1dt));
  1 个评论
KSSV
KSSV 2019-3-8
X=-10:.1:10;
T=-10:.1:10;
mu1=-.01+1*1i;
a=(.1-2*1i);
b=(.1-.1*1i);
[x,t]=meshgrid(X,T);
x1=exp(-1i*mu1.*x).*exp((1-(1i./(2*mu1)).*t));
x2=exp(1i*mu1.*x).*exp((1+(1i./(2*mu1)).*t));
y1=exp(-1i*mu1.*x).*exp((1+(1i./(2*mu1)).*t));
y2=exp(1i*mu1.*x).*exp((1-(1i./(2*mu1)).*t));
A=2*1i*(1)*a*conj(b)*x1.*conj(x2).*(x2.*conj(y1)-y2.*conj(x1));
B=(a.*conj(a).*x1.*conj(x1).*y2.*conj(y2)+b.*conj(b).*x1.*conj(y1).*x2.*conj(y2)+a.*conj(a).*x2.*conj(x2).*y1.*conj(y1)+b.*conj(b).*y1.*conj(x2).*y2.*conj(x1));
r1=-2.*1i.*((A./B));
dr1=gradient(r1);
dt=gradient(t);
dr1dt=dr1./dt;
td=t(2:end);
surf(x,t,abs(dr1dt)');
But you need re think on your code.

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2019-3-8
X=-10:.1:10;
T=-10:.1:10;
mu1=-.01+1*1i;
a=(.1-2*1i);
b=(.1-.1*1i);
[x,t]=meshgrid(X,T);
x1=exp(-1i*mu1.*x).*exp((1-(1i./(2*mu1)).*t));
x2=exp(1i*mu1.*x).*exp((1+(1i./(2*mu1)).*t));
y1=exp(-1i*mu1.*x).*exp((1+(1i./(2*mu1)).*t));
y2=exp(1i*mu1.*x).*exp((1-(1i./(2*mu1)).*t));
A=2*1i*(1)*a*conj(b)*x1.*conj(x2).*(x2.*conj(y1)-y2.*conj(x1));
B=(a.*conj(a).*x1.*conj(x1).*y2.*conj(y2)+b.*conj(b).*x1.*conj(y1).*x2.*conj(y2)+a.*conj(a).*x2.*conj(x2).*y1.*conj(y1)+b.*conj(b).*y1.*conj(x2).*y2.*conj(x1));
r1=-2.*1i.*((A./B));
dr1=gradient(r1);
dt=gradient(t);
dr1dt=dr1./min(diff(T));
td=t(2:end);
surf(x,t,abs(dr1dt)');
To get dt you can use difference in T. You need not to take a matrix. ANote that dt is same i.e 0.01. If you use a matrix..it is coming out to be zero matrix and makes dr1dt a nan or inf matrix. I advice you to still rethink on your code.
  5 个评论
KSSV
KSSV 2019-3-8
diff reduces the dimension by one....ad you are subtracting the consecutive elements. Gradient will not reduce the dimensions.
Wajahat
Wajahat 2019-3-8
编辑:Wajahat 2019-3-9
@ KSSV,
To verify, I considered a simple example such as
X=-1:.05:1;
T=-1:.05:1;
mu1=1+1*1i;
[x,t]=meshgrid(X,T);
r1=mu1.*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;
mu1=1+1*1i;
[x,t]=meshgrid(X,T);
r1=4.*mu1.*cos(x+4.*t);
surf(x,t,abs(r1)');
You can see there is difference of amplitude in these plots. Why the two plots are not identical?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by