Calling a function inside ode45, yet sending the function extra variables, how do I do this...

4 visualizaciones (últimos 30 días)
What I'm trying to do is create a script that will define (later I'll have it prompt) certain variables values (physical constants such as mass, coefficient of drag etc..), and use these values inside a function that I call. The function I'm calling will be inside the ode45 expression though. See the script below code below:
% code
clear all
m=80
c=1.2
span = 0:.0001:2.8665;
initialv=28
icy=[4 initialv*sind(30)];
icx=[0 initialv*cosd(30)];
[t,y]=ode45(@f,span,icy)
[t,x]=ode45(@fx,span,icx)
end
What I want to do is call the function f, and fx, and send the values of m and c to be used in it. Here is the function f:
% code
function fy = f(t,y)
c=1.2
m=80;
g=9.81
fy=[y(2); -g-c*y(2).^2/m];
end
In other words, I'd like to not have to modify my function every time I want to change these physical constants. I'd like to be able to set those constants in the script, and then feed them into the function to use (on the fly). This way I can have a script where I input all my variables, and it makes the necessary changes inside my function as well. Sorry if this was unclear. I appreciate any help!

Respuestas (1)

Walter Roberson
Walter Roberson el 15 de Abr. de 2013

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by