Error running ode45 code
1 回表示 (過去 30 日間)
古いコメントを表示
Hi
I write this code,however, I can't run this code. please correct this code for me
clc
clear all
[aa kk]=ode45(@fy,[0.1 0.2],2);
function dadk=fy(k,a)
syms a b
F=(a^6-b^6)*sin(a)*sinh(b)+2*a^3*b^3*cos(a)*cosh(b)-2*a^3;
x=diff(F,a);
y=diff(F,b)
clear b
syms a k
b=a*(1/(a^2+k^2+1))^0.5;
z=diff(b,a);
w=diff(b,k)
dadk=-(y*w)./(x+y*z)
1 件のコメント
回答 (1 件)
Walter Roberson
2013 年 12 月 15 日
編集済み: Walter Roberson
2013 年 12 月 15 日
Why are you passing "a" into fy(), but then overriding its value to use "a" as a symbol?
If you do not care what the value of the parameter is, then name it something that is not otherwise used in the routine so that the reader does not get confused.
If you do mean to use the value passed in for "a" then do not also use "a" as a symbolic variable.
Likewise, you have the same problem with regards to "k".
The value you compute, dadk, is symbolic because you were working symbolically, But you cannot return a symbolic expression from your fy() because ode45 needs to work numerically.
When you calculate x and y, you do so with respect to a symbol "b". But then you clear "b". When you assign to "b" a moment later, do you mean a different "b", or do you mean the same one? If you meant the same one, why did you "clear b", and so give the hint to the reader that the two "b" are unrelated?
I have a suspicion I know what you want to do. What I think you should probably do is perform the entire symbolic calculation before you call fy(), and then simple() the expression, and call matlabFunction() on the result, using the option
'vars', {k, a}
Then take the resulting function handle and pass it in to ode45 instead of the fy() that you have now.
syms a b k
F = (a^6-b^6) * sin(a) * sinh(b) + 2 * a^3 * b^3 * cos(a) * cosh(b) - 2 * a^3;
x = diff(F,a);
y = diff(F,b)
b = a * sqrt(1/(a^2 + k^2 + 1));
z = diff(b,a);
w = diff(b,k)
dadk = -(y*w)./(x+y*z)
fy = matlabFunction( simple(dadk), 'vars', {k, b});
[aa kk] = ode45(fy, [0.1 0.2], 2);
2 件のコメント
Walter Roberson
2013 年 12 月 15 日
Your equation in the PDF has b as a * sqrt(1/(a^2 * k^2 + 1)) but your code has b as a * sqrt(1/(a^2 + k^2 + 1)); You will need to resolve whether it is a^2*k^2 or a^2+k^2
参考
カテゴリ
Help Center および File Exchange で Symbolic Math Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!