You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB新手求助:如何绘制位置、速度、加速度随θ变化曲线

How to Compute Velocity & Acceleration Curves in MATLAB for Your Position Function

Hey there! Since you're just starting out with MATLAB today, let's break this down step by step to get your velocity and acceleration curves plotted right next to your position data.

First, let's clarify: Your position x is a function of the angle θ, so velocity (the rate of change of x with respect to θ) and acceleration (the second derivative of x with respect to θ) can be calculated two main ways—symbolic math (exact derivatives) or numerical differentiation (approximating derivatives from your discrete θ array). We'll cover both, starting with symbolic math since it's more intuitive for beginners.


Method 1: Symbolic Math (Exact Derivatives)

MATLAB's Symbolic Math Toolbox lets you define equations symbolically and compute derivatives directly. Here's how to apply it to your problem:

  1. Define symbolic variables and constants
    First, tell MATLAB which variables are symbolic (i.e., theta, plus your constants b, d, h):

    syms theta b d h  % Define symbolic variables
    
  2. Write your position function symbolically
    Recreate your beta and x equations using these symbolic variables:

    beta = asin((h + b*cos(theta))/d);
    x_sym = b*cos(theta) + d*cos(beta);
    
  3. Compute first (velocity) and second (acceleration) derivatives
    Use the diff() function to take derivatives of x_sym with respect to theta:

    x_dot_sym = diff(x_sym, theta);  % First derivative (velocity w.r.t θ)
    x_dd_sym = diff(x_dot_sym, theta);  % Second derivative (acceleration w.r.t θ)
    
  4. Convert symbolic functions to numerical functions
    To use these with your discrete theta array, convert the symbolic expressions to anonymous functions using matlabFunction. Don't forget to plug in your actual constant values here:

    % Replace these with your real constant values (e.g., b=0.1, d=0.2, h=0.05)
    b_val = 0.1;
    d_val = 0.2;
    h_val = 0.05;
    
    x_fun = matlabFunction(x_sym, 'Vars', {theta, b, d, h});
    x_dot_fun = matlabFunction(x_dot_sym, 'Vars', {theta, b, d, h});
    x_dd_fun = matlabFunction(x_dd_sym, 'Vars', {theta, b, d, h});
    
  5. Calculate numerical values for your theta array
    Use your original theta array to compute x, x_dot, and x_dd:

    theta = 0:1:pi;  % Your original theta array (in radians)
    theta_deg = rad2deg(theta);  % Convert to degrees for plotting
    
    x = x_fun(theta, b_val, d_val, h_val) * 1000;  % Position in mm
    x_dot = x_dot_fun(theta, b_val, d_val, h_val) * 1000;  % Velocity (mm/rad)
    x_dd = x_dd_fun(theta, b_val, d_val, h_val) * 1000;  % Acceleration (mm/rad²)
    
  6. Plot all three curves
    Use subplots to keep everything organized and easy to read:

    figure('Name','Position, Velocity, Acceleration vs θ');
    
    % Subplot 1: Position
    subplot(3,1,1);
    plot(theta_deg, x, 'b-', 'LineWidth',2);
    title('Position vs θ');
    ylabel('Position (mm)');
    grid on;
    
    % Subplot 2: Velocity
    subplot(3,1,2);
    plot(theta_deg, x_dot, 'r-', 'LineWidth',2);
    title('Velocity vs θ');
    ylabel('Velocity (mm/rad)');
    grid on;
    
    % Subplot 3: Acceleration
    subplot(3,1,3);
    plot(theta_deg, x_dd, 'g-', 'LineWidth',2);
    title('Acceleration vs θ');
    xlabel('θ (degrees)');
    ylabel('Acceleration (mm/rad²)');
    grid on;
    

Method 2: Numerical Differentiation (Approximate Derivatives)

If you don't have the Symbolic Math Toolbox, you can use numerical differentiation with MATLAB's diff() function. Note that this gives approximate results, and the output array will be one element shorter than your original theta array:

  1. Calculate numerical derivatives

    theta = 0:1:pi;
    theta_deg = rad2deg(theta);
    b_val = 0.1; d_val = 0.2; h_val = 0.05;
    
    % Compute original position array
    beta = asin((h_val + b_val*cos(theta))/d_val);
    x = (b_val*cos(theta) + d_val*cos(beta)) * 1000;
    
    % Compute velocity (dx/dθ) using diff
    dtheta = theta(2) - theta(1);  % Step size of theta
    x_dot = diff(x) / dtheta;  % Velocity (mm/rad)
    theta_dot_deg = theta_deg(1:end-1);  % Match length with x_dot
    
    % Compute acceleration (d²x/dθ²) using diff again
    x_dd = diff(x_dot) / dtheta;  % Acceleration (mm/rad²)
    theta_dd_deg = theta_deg(1:end-2);  % Match length with x_dd
    
  2. Plot numerical results

    figure('Name','Numerical Position, Velocity, Acceleration vs θ');
    
    subplot(3,1,1);
    plot(theta_deg, x, 'b-', 'LineWidth',2);
    title('Position vs θ');
    ylabel('Position (mm)');
    grid on;
    
    subplot(3,1,2);
    plot(theta_dot_deg, x_dot, 'r-', 'LineWidth',2);
    title('Velocity vs θ');
    ylabel('Velocity (mm/rad)');
    grid on;
    
    subplot(3,1,3);
    plot(theta_dd_deg, x_dd, 'g-', 'LineWidth',2);
    title('Acceleration vs θ');
    xlabel('θ (degrees)');
    ylabel('Acceleration (mm/rad²)');
    grid on;
    

Quick Notes for You:

  • If you actually want velocity/acceleration with respect to time (not θ), you'll need to define how θ changes over time (e.g., theta = omega * t where omega is angular velocity in rad/s). Then, x_dot_time = x_dot * omega and x_dd_time = x_dd * omega^2.
  • Make sure your constants b, d, h are in consistent units (e.g., meters) before multiplying by 1000 to get millimeters.

内容的提问来源于stack exchange,提问作者Sunden

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:23:34