필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Need Help Transcribing differential equations in to Matlab code

조회 수: 1 (최근 30일)
JB
JB 2018년 7월 6일
마감: MATLAB Answer Bot 2021년 8월 20일
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.

답변 (2개)

Fabricio Castro
Fabricio Castro 2018년 7월 7일
Hi JB
Your problem is kind of simple to solve. With the following code you will achieve what you want:
%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)
Thanks
  댓글 수: 2
Star Strider
Star Strider 2018년 7월 7일
@Fabricio Castro — We do not supply complete code to homework problems here on MATLAB Answers. The OP must demonstrate an effort. We will then help to get the posted code running.
JB
JB 2018년 7월 7일
@Star_Strider - I appologize. Here is my first attempt at the code:
"func1.m" file: function dy = func1(t, y) dy = (m*g)/(K*(1-e^(-K/(m*t)))); end
Then calling it using ode45: [t,y] = ode45(@func1, [I have no Idea what I am doing here], y0);
I will remember to post my first attempt in my next question.
Sorry about that.
@Fabricio_Castro - Thank you for the assistance. As you can see, I am trying to create a function of the equation and then call it using ode45 and the plot the graph.
Any assistance would be greatly appreciated. I am very new to Matlab.
V/R JB

Star Strider
Star Strider 2018년 7월 7일
The syntax of your function is correct. However you need to spend some time with the documentation.
Specifically see the documentation on the exp (link) function.
For future reference, see the documentation on Anonymous Functions (link), and Vectorization (link).

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by