plotting a derivative of a function using surf command

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

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, sorry, I don't understand it. Why you use 'min(diff(T))' instead of 'dt'
YOur time step is alway constant......check diff(T)
@ KSSV, also what is the reason to use 'gradient' instead of 'diff' in the code?
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?

댓글을 달려면 로그인하십시오.

카테고리

질문:

2019년 3월 8일

편집:

2019년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by