I keep receiving "Error using / Arguments must be numeric, char, or logical." when I try to find value of sym function

I am trying to solve a system of ODE's using the Runge Kutta Method and I keep receiving the above error when I try to run it. Any help would be much appreciated!
d = @(t) sqrt(d0^2 -k*t*(d0)^2);
m = @(t) 1000*(1/6)*d^3;
Fd2 = @(t,y,v) (18*pi*v)/(1000*(d^2)) ;
F1 = @(t,y,v) v ;%dy/dt = v
F2 = @(t,y,v) Fd2/m - 9.8 % dv/dt = -g + Fd/m
h = 1;
t = 0:h:15; %Analyzing up to t = 15.
v = zeros(1,length(t));
y = zeros(1,length(t));
v(1) = 0;
y(1) = 2000;
for i = 1:1:length(t) - 1
k1 = F1(t(i),y(i),v(i))
p1 = F2(t(i),y(i),v(i))
k2 = F1(t(i)+h/2, y(i)+(h/2)*k1, v(i)+(h/2)*p1)
p2 = F2(t(i)+h/2, y(i)+(h/2)*k1, v(i)+(h/2)*p1)
k3 = F1(t(i)+h/2, y(i)+(h/2)*k2, v(i)+(h/2)*p2)
p3 = F2(t(i)+h/2, y(i)+(h/2)*k2, v(i)+(h/2)*p2)
k4 = F1(t(i)+h, y(i)+(h)*k3, v(i)+(h)*p3)
p4 = F2(t(i)+h, y(i)+(h)*k3, v(i)+(h)*p3)
y(i+1) = y(1) + h * (k1+2*k2+2*k3+k4)/6
v(i+1) = v(i) + h * (p1+2*p2+2*p3+p4)/6
end
The following is the full error I receive:
Error using /
Arguments must be numeric, char, or logical.
Error in RK>@(t,y,v)Fd2/m-9.8 (line 30)
F2 = @(t,y,v) Fd2/m - 9.8 % dv/dt = -g + Fd/m
Error in RK (line 43)
p1 = F2(t(i),y(i),v(i))

 Réponse acceptée

If a function calls another function that takes input variables, you need to define the function to pass the variables through to the other function.
So, change
F2 = @(t,y,v) Fd2/m - 9.8
to
F2 = @(t,y,v) Fd2(t,y,v)/m(t) - 9.8
Also, change
m = @(t) 1000*(1/6)*d^3;
to
m = @(t) 1000*(1/6)*d(t)^3;

3 commentaires

Thank you so much, your answer was very helpful. Unfortunately, as soon as I changed my functions, another error arose:
I receive imaginary numbers for the y(i) values, so somewhere somehow the code messes up the functions.
Any help you can provide would be amazing :)
@Michal Amar You're welcome. So, with the function definitions corrected, it seems your problem has shifted from a syntactic error to a semantic error. Semantic errors are generally trickier to resolve.
There are lot of calculations to navigate in your code. Offhand, I don't see any obvious problem. I suggest you post the new problem (unexpected imaginary numbers) as a new question. A fresh set of eyes might help. Good luck.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by