フィルターのクリア

ODE function in a loop

1 回表示 (過去 30 日間)
Physics
Physics 2017 年 3 月 2 日
コメント済み: Walter Roberson 2017 年 3 月 4 日
I have a function of x and t, and am creating a loop to find the average x/t for every U. I have successfully used this method before, however my x values are zero every time.
My function is:
function dx=myfunction(t,x,U)
de=24.35;
dd=294.5.*10^(-9);
hbar=6.5.*10^(-16);%
lambda=20*d;
k=(2.*pi)/(lambda);
v=2.5.*10^(-3);
dx=zeros(3,1);
dx(1)=((de)/(2.*hbar.*v.*k)).*sin(x(2));
dx(2)=(dd.*U)./(hbar.*2*v).*(cos(((pi.*x(1))/10)-(t)));
end
In the loop:
dU=1;
U=0:dU:200;%
tspan=[1 30];
vx=zeros(size(U));
for j=1:length(U)
[t,x]=ode45(@(t,x) myfunction(t,x,U(j)),tspan,[0 0 0]);
vx(j)=(sum((x(:,1))./(t))./(length(t));
end
Any ideas as to why the changing of U gives x a value of 0 would be useful.
Thanks in advance.
  3 件のコメント
Physics
Physics 2017 年 3 月 2 日
編集済み: Physics 2017 年 3 月 2 日
I have edited the constants and names. Changed it for ease.
Jan
Jan 2017 年 3 月 3 日
Note: While dd=294.5.*10^(-9) is an exüpensive power operation, dd=294.5e-9 is a cheap constant.

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

回答 (1 件)

Jan
Jan 2017 年 3 月 3 日
The code will not run at all, because the number of parenthesis in
vx(j) = (sum((x(:,1))./(t))./(length(t));
is wrong. I guess you want:
vx(j) = sum(x(:,1) ./ t) ./ length(t);
It is useful to use additional parenthesis, if it improves the readability of the code, but here this was an overdoing.
If this is fixed, runnign your code fails in the line:
lambda=20*d;
due to an undefined variable "d".
Finally all I can suggest to use the debugger to examine, what's going on. Set a breakpoint in your code and step through the program line by line. This helps to understand, where which values are defined. Please post the running code afterwards, if you still have problems.
  2 件のコメント
Physics
Physics 2017 年 3 月 3 日
Thanks so much for taking the time to look at this.
These issues again are from my editing to create this post - they would clearly show as errors when running!
I was getting 0 values from the size of my range, as a solution for your interest.
Thanks again!
Walter Roberson
Walter Roberson 2017 年 3 月 4 日
If you are still having difficulty, please post corrected code.

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by