Suppose, v(x) is a user-defined function. How, to get v(-x) which is going to be used in the same code?

Is there any in-built MATLAB function to do it. Should we define user-defined function, say, a local function. If so, how that can be done?

回答 (2 件)

Hi,
sorry, I don't understand the question. If you have the function, why don't you simply call v(-x)?
Or are you looking for defining this function? In this case you could do e.g.
minusv = @(x) v(-x);
Titus

1 件のコメント

Let me give you a part of the code I used:
L=80; N=4096; dt=0.0001; tmax=20; nmax=round(tmax/dt); dx=L/N; x=[-L/2:dx:L/2-dx]'; k=[0:N/2-1 -N/2:-1]'*2*pi/L; k2=k.^2;
a=0.0;
y=-x-a;
u=sech(x);
ucon=sech(y);
udata=u; tdata=0;
for nn=1:nmax % Integration begins
du1=1i*(-ifft(0.5*k2.*fft(u))+u.*u.*conj(ucon)); v=u+0.5*du1*dt;
du2=1i*(-ifft(0.5*k2.*fft(v))+v.*v.*conj(vcon)); v=u+0.5*du2*dt;
du3=1i*(-ifft(0.5*k2.*fft(v))+v.*v.*conj(vcon)); v=u+ du3*dt;
du4=1i*(-ifft(0.5*k2.*fft(v))+v.*v.*conj(vcon));
u=u+(du1+2*du2+2*du3+du4)*dt/6;
if mod(nn,round(nmax/25)) == 0
udata=[udata u]; tdata=[tdata nn*dt];
%end
end
ucon=u;
end
subplot(2,2,1)
imagesc(x,tdata,abs(udata'));
colorbar
title('Evolution of the rogue wave')
ylabel('Time','fontsize',12)
xlabel('Transverse co-ordinate','fontsize',12)
My question is as follows: in the integration part by Runge-Kutta method, ucon is being calculated by the usual way as it has been defined ucon=conj(u(-x)). Since, my u(x)=sech(x), there is no as such problem for matlab to calculate ucon. Then using du1, it finds v, using that when it calculates du2, it needs information of vcon=v(-x). Now, the form of v(x) is not fully known. So, my question is how to define and v(-x) so that it can be called and used in the code to get expected result.
Thanks and regards,
Samit

サインインしてコメントする。

Stephen23
Stephen23 2015 年 2 月 13 日
編集済み: Stephen23 2015 年 2 月 13 日
You can't define v(x) as a function , only v could be defined as a function.
Once you define a function v, you can call it with either v(x) or v(-x) or any other suitable values that you want to use. Calling a function is independent of how it is defined:
>> A = @(x)disp(x); % define a function A
>> A(3)
3
>> A(-3)
-3

1 件のコメント

Let me give you a part of the code I used:
L=80; N=4096; dt=0.0001; tmax=20; nmax=round(tmax/dt); dx=L/N; x=[-L/2:dx:L/2-dx]'; k=[0:N/2-1 -N/2:-1]'*2*pi/L; k2=k.^2;
a=0.0;
y=-x-a;
u=sech(x);
ucon=sech(y);
udata=u; tdata=0;
for nn=1:nmax % Integration begins
du1=1i*(-ifft(0.5*k2.*fft(u))+u.*u.*conj(ucon)); v=u+0.5*du1*dt;
du2=1i*(-ifft(0.5*k2.*fft(v))+v.*v.*conj(vcon)); v=u+0.5*du2*dt;
du3=1i*(-ifft(0.5*k2.*fft(v))+v.*v.*conj(vcon)); v=u+ du3*dt;
du4=1i*(-ifft(0.5*k2.*fft(v))+v.*v.*conj(vcon));
u=u+(du1+2*du2+2*du3+du4)*dt/6;
if mod(nn,round(nmax/25)) == 0
udata=[udata u]; tdata=[tdata nn*dt];
%end
end
ucon=u;
end
subplot(2,2,1)
imagesc(x,tdata,abs(udata'));
colorbar
title('Evolution of the rogue wave')
ylabel('Time','fontsize',12)
xlabel('Transverse co-ordinate','fontsize',12)
My question is as follows: in the integration part by Runge-Kutta method, ucon is being calculated by the usual way as it has been defined ucon=conj(u(-x)). Since, my u(x)=sech(x), there is no as such problem for matlab to calculate ucon. Then using du1, it finds v, using that when it calculates du2, it needs information of vcon=v(-x). Now, the form of v(x) is not fully known. So, my question is how to define and v(-x) so that it can be called and used in the code to get expected result.
Thanks and regards,
Samit

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeSimulink についてさらに検索

質問済み:

2015 年 2 月 13 日

コメント済み:

2015 年 2 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by