I need help fully transcribing my ODE to Matlab
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Originally I set out to do this: I am trying to transcribe the following differential equation that describes a falling body:
dy/dt=mg/K (1-e^(-K/m t)) (with air resistance)
dy/dt=gt (without air resistance)
I would like to create a function and then call the function using ode45 and then plot a graph. I am still trying to get a good handle of matlab, but I have not been successful.
My intent is to create a file called FallObj.m with the equation in a function, call the equation using ode45 and the associated parameters, and plot the graph of the output.
My first attempt:
function dy = func1(t, y)
dy = (m*g)/(K*(1-e^(-K/(m*t))));
end
[t,y] = ode45(@func1, [I have no Idea what I am doing here], y0);
Then I received some help:
%Supose you may want to use a time interval of [0,3] with an initial
%condition y0 equal to 50 meters
timeInterval = linspace(0,3,51);
y0 = 50;
%Defining parameters like mass (m), air resistance (K), and gravity (g)
m = 5;
K = 2;
g = 9.8;
%Using ode45 function to solve the falling body equation
[t,y] = ode45(@(t,y) (m*g)/(K*(1-exp(-K/(m*t)))), timeInterval, y0);
%Plot the result
figure
subplot(1,2,1)
plot(t,((m.*g)./(K.*(1-exp(-K./(m.*t))))),'-bo')
xlabel('time')
ylabel('dy/dt')
title('ODE graph')
set(gca,'FontSize',18)
subplot(1,2,2)
plot(t,y,'-ro')
xlabel('time')
ylabel('y')
title('Solution graph')
set(gca,'FontSize',18)
Here is my update:
function dy = FallObj(t, y)
m = 5;
K = 0.125;
g = 9.8;
y0 = 50;
dy = (m*g)/(K*(1-exp(-K/(m*t))));
end
I keep getting the following error:
Not enough input arguments.
Error in FallObj (line 6)
dy = (m*g)/(K*(1-exp(-K/(m*t))));
Any assistance would be greatly appreciated. I think I am beginning to confuse myself.
V/R JB
0 comentarios
Respuestas (1)
Star Strider
el 7 de Jul. de 2018
I can only guess as to the reason the error continues to be thrown. You could have ‘overshadowed’ the exp function somehow, by creating your own function with the same name and a different number of input arguments.
Type this in your Command Window:
which('exp', '-all')
All the valid results should be in sub-directories of the ‘C:\Program Files\MATLAB\R2018a\toolbox\’ main directory. If any are not, that is the problem. The solution is to re-name the function you created.
That aside, I would create one anonymous function for both of your falling object models:
FallObj = @(t,y,m,g,K) [m*g/K*(1-exp(-K*t/m)); g*t];
These are independent of one another, so you get both results in one call to ode45. Remember that to use it, your initial conditions vector must have 2 elements, one for each differential equation.
The rest should be straightforward.
0 comentarios
Ver también
Categorías
Más información sobre Ordinary Differential Equations 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!