Solve F=ma for position. I need to solve 2nd order differential equation in MATLAB and I read all the tutorials out there I still can't figure out how to do it, Please help!

조회 수: 7 (최근 30일)
I know I have F=ma=m*dv/dt=m*d^2x/dt^2 and I need first to solve for velocity by using ode23 once, then use ode23 again on whatever that answer is to solve for position, but I don't know how to set it up in a code, please help!

답변 (1개)

BhaTTa
BhaTTa 2024년 7월 19일
To solve for velocity and then position using ode23 in MATLAB, you need to set up two separate differential equations. The first differential equation will be for velocity, and the second one will be for position. Here's a step-by-step guide to achieve this:Step 1: Define the Differential Equations
  1. Velocity Equation: ( \frac{dv}{dt} = \frac{F}{m} )
  2. Position Equation: ( \frac{dx}{dt} = v )
Step 2: Use ode23 to Solve for Velocity
First, solve the differential equation for velocity.
Step 3: Use ode23 to Solve for Position
Next, use the result from the velocity solution to solve for position.
Example Code
Here's a complete MATLAB code example to solve for velocity and position using ode23:
% Clear workspace and command window
close all;
clear all;
clc;
% Define constants
m = 1; % Mass (kg)
F = 10; % Force (N)
% Define the time span for the simulation
tspan = [0 10]; % From 0 to 10 seconds
% Initial conditions
v0 = 0; % Initial velocity (m/s)
x0 = 0; % Initial position (m)
% Define the differential equation for velocity
dvdt = @(t, v) F / m;
% Solve for velocity using ode23
[t_v, v] = ode23(dvdt, tspan, v0);
% Define the differential equation for position
dxdt = @(t, x) interp1(t_v, v, t); % Interpolate velocity values
% Solve for position using ode23
[t_x, x] = ode23(dxdt, tspan, x0);
% Plot the results
figure;
subplot(2, 1, 1);
plot(t_v, v, 'r-', 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Velocity (m/s)');
title('Velocity vs Time');
grid on;
subplot(2, 1, 2);
plot(t_x, x, 'b-', 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Position (m)');
title('Position vs Time');
grid on;
  댓글 수: 2
Sam Chak
Sam Chak 2024년 7월 19일
If the force isn't a constant, can you show how to put this stabilizing force equation into dvdt = @(t, v) in the code?
James Tursa
James Tursa 2024년 7월 19일
편집: James Tursa 2024년 7월 19일
@Sam Chak +1 for pointing out the flaw in this proposed solution if F depends on position.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by