Clear Filters
Clear Filters

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!

5 views (last 30 days)
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!

Answers (1)

BhaTTa
BhaTTa on 19 Jul 2024
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;

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!