필터 지우기
필터 지우기

Error running ode45 code

조회 수: 1 (최근 30일)
Jamal
Jamal 2013년 12월 15일
댓글: Walter Roberson 2013년 12월 15일
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
Walter Roberson
Walter Roberson 2013년 12월 15일
code that contains "clear all" should be assumed to be wrong.

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

답변 (1개)

Walter Roberson
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
Jamal
Jamal 2013년 12월 15일
편집: Jamal 2013년 12월 15일
thank you about your answer
F is function of 'a' and 'b'(b is a function of 'a' and 'k')
i want to solve this ode
diff(a,k)=(diff(F,b)*diff(b,k))/(diff(F,a)+diff(F,b)*diff(b,a))
right hand of equation is based on a and k variables
I want to plot "a" versus "k"
I attached my my differential equation
please help me to solve this problem
Walter Roberson
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 CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by