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!