MATLAB新手求助:如何绘制位置、速度、加速度随θ变化曲线
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:
Define symbolic variables and constants
First, tell MATLAB which variables are symbolic (i.e.,theta, plus your constantsb,d,h):syms theta b d h % Define symbolic variablesWrite your position function symbolically
Recreate yourbetaandxequations using these symbolic variables:beta = asin((h + b*cos(theta))/d); x_sym = b*cos(theta) + d*cos(beta);Compute first (velocity) and second (acceleration) derivatives
Use thediff()function to take derivatives ofx_symwith respect totheta: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 θ)Convert symbolic functions to numerical functions
To use these with your discretethetaarray, convert the symbolic expressions to anonymous functions usingmatlabFunction. 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});Calculate numerical values for your theta array
Use your originalthetaarray to computex,x_dot, andx_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²)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:
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_ddPlot 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 * twhereomegais angular velocity in rad/s). Then,x_dot_time = x_dot * omegaandx_dd_time = x_dd * omega^2. - Make sure your constants
b,d,hare in consistent units (e.g., meters) before multiplying by 1000 to get millimeters.
内容的提问来源于stack exchange,提问作者Sunden

