Filter löschen
Filter löschen

Error running ode45 code

1 Ansicht (letzte 30 Tage)
Jamal
Jamal am 15 Dez. 2013
Kommentiert: Walter Roberson am 15 Dez. 2013
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 Kommentar
Walter Roberson
Walter Roberson am 15 Dez. 2013
code that contains "clear all" should be assumed to be wrong.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 15 Dez. 2013
Bearbeitet: Walter Roberson am 15 Dez. 2013
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 Kommentare
Jamal
Jamal am 15 Dez. 2013
Bearbeitet: Jamal am 15 Dez. 2013
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 am 15 Dez. 2013
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by