Programming with DeepSeek

Today, I logged into DeepSeek to explore what all the excitement was about. Curious to see how it compared to ChatGPT, I tested it by asking it to interpret some of my MATLAB code. Unfortunately, any attempt with code exceeding 300 lines resulted in the message: "The server is busy. Please try again later."—which was a bit disappointing.

Undeterred, I tried a smaller function with just 50 lines of code. This time, the output was generated in just 2-3 seconds, and I have to say, the results were quite impressive!

This MATLAB function, approach1_AR2, is designed to analyze a force time series (a sequence of force measurements over time) and estimate the parameters of a second-order mechanical system (mass-spring-damper system) using an autoregressive (AR) model. Here's a breakdown of what the program does:

Purpose

The function estimates the mass (M), damping coefficient (C), and stiffness (K) of a second-order system by analyzing the force time series. It uses an AR(2) model (a second-order autoregressive model) to approximate the system dynamics.

Inputs

1. `forceVec`: A vector containing the force time series data (e.g., force measurements over time).

2. `Fs`: The sampling frequency (in Hz) of the force time series.

Outputs

The function returns three values:

1. `mVal`: Estimated mass (M) of the system.

2. `cVal`: Estimated damping coefficient (C) of the system.

3. `kVal`: Estimated stiffness (K) of the system.

If the function encounters an error or cannot compute valid estimates, it returns NaN (Not-a-Number) for all three outputs.

Steps in the Program

1. Remove DC Offset:

  - The mean of the force time series is subtracted to remove any constant offset (DC component), leaving only the dynamic part of the signal.

2. Fit an AR(2) Model:

  - A second-order autoregressive (AR) model is fitted to the detrended force data. The AR model describes the force at time k as a linear combination of the force at previous time steps (`k-1` and k-2).

3. Extract Poles:

  - The roots of the AR model's characteristic polynomial are computed. These roots represent the discrete-time poles of the system.

4. Identify Dominant Pole:

  - The dominant pole (the one with the largest magnitude) is selected. This pole is assumed to represent the primary dynamics of the system.

5. Convert Discrete Pole to Continuous-Time:

  - The dominant discrete-time pole is converted to a continuous-time pole using a logarithmic transformation. This step maps the discrete-time system to a continuous-time system.

6. Compute Natural Frequency and Damping Ratio:

  - The continuous-time pole is used to calculate the natural frequency (`wn`) and damping ratio (`zeta`) of the system.

7. Estimate M, C, and K:

  - Using the natural frequency and damping ratio, the mass (M), damping coefficient (C), and stiffness (K) are estimated using the following relationships:

   - \( M = \frac{1}{\omega_n^2} \)

   - \( K = 1 \) (normalized stiffness)

   - \( C = 2 \zeta \sqrt{M \cdot K} \)

8. Error Handling:

  - If any step fails (e.g., insufficient data, invalid poles, or numerical issues), the function returns NaN for all outputs.

Key Assumptions

1. The force time series can be modeled as a second-order system (mass-spring-damper).

2. The AR(2) model is sufficient to capture the dominant dynamics of the system.

3. The system is stable (poles have negative real parts in continuous time).

Applications

This function could be used in fields like:

- Biomechanics (e.g., analyzing human movement or muscle dynamics).

- Mechanical systems (e.g., estimating parameters of a vibrating structure).

- Signal processing (e.g., modeling and analyzing time series data).

Limitations

1. The function assumes a second-order system. If the system is higher-order or nonlinear, the estimates may not be accurate.

2. The AR(2) model may not capture all dynamics if the system has significant noise or complex behavior.

3. The normalization of stiffness (`K = 1`) is a simplification and may not reflect the true physical system.

In summary, this function uses an AR(2) model to estimate the mass, damping, and stiffness of a second-order system from force time series data. It is a mathematical approach to approximate physical system parameters from observed data.

Next, I asked ChatGPT-4 to analyze the same code segment and here are the results.


Here is an analysis of the provided MATLAB code:


Function Overview

The function approach1_AR2 estimates the mass (M), damping coefficient (C), and stiffness (K) of a second-order system (mass-spring-damper) by analyzing a force time-series input. It employs an autoregressive (AR) model of order 2 to interpret system dynamics through the roots of its characteristic polynomial. These roots (or poles) are then converted to continuous-time representation, allowing the calculation of natural frequency (ωn\omega_n) and damping ratio (ζ\zeta).


Key Steps in the Code

1. Initialization

  • Outputs for mass (MM), damping (CC), and stiffness (KK) are initialized to NaN to ensure safe handling of errors.

2. Remove DC Offset

  • The mean of the input force vector (forceVec) is subtracted to remove any constant offset, isolating the dynamic component for AR modeling.

3. AR(2) Model Fitting

  • A second-order autoregressive model is fitted to the detrended force data. The AR model is represented as: y(k)+a1y(k−1)+a2y(k−2)=e(k)y(k) + a_1 y(k-1) + a_2 y(k-2) = e(k) where a1a_1 and a2a_2 are the model coefficients.

4. Polynomial Coefficients and Pole Extraction

  • The characteristic polynomial of the AR model is defined as: A(z−1)=1+a1z−1+a2z−2A(z^{-1}) = 1 + a_1 z^{-1} + a_2 z^{-2}
  • The roots of this polynomial represent the discrete-time poles (zz) of the system.

5. Dominant Pole Selection

  • The pole with the largest magnitude is assumed to represent the dominant dynamics of the system. This simplification may work for systems where a single mode dominates.

6. Discrete-to-Continuous Conversion

  • The dominant discrete-time pole is transformed into a continuous-time representation using: s=ln(z)Δts = \frac{\ln(z)}{\Delta t} where Δt=1Fs\Delta t = \frac{1}{F_s} is the sampling interval.

7. Natural Frequency (ωn\omega_n) and Damping Ratio (ζ\zeta)

  • The natural frequency is computed as: ωn=∣s∣\omega_n = |s|
  • The damping ratio is given by: ζ=−Re(s)ωn\zeta = -\frac{\text{Re}(s)}{\omega_n}

8. System Parameter Estimation

  • Using the calculated natural frequency and damping ratio, the system parameters are approximated: M=1ωn2,K=1,C=2ζM⋅KM = \frac{1}{\omega_n^2}, \quad K = 1, \quad C = 2\zeta\sqrt{M \cdot K}
  • The stiffness is normalized to K=1K = 1, simplifying the calculations but potentially limiting the generality of the model.

9. Error Handling

  • If any step fails (e.g., due to unstable poles, invalid damping ratio, or insufficient data), the function exits gracefully, returning NaN for all outputs.


Strengths

  1. Efficient System Identification: The use of AR modeling simplifies the estimation of second-order system dynamics.
  2. Error Handling: Robust handling of invalid inputs or outputs ensures the function doesn't crash in edge cases.
  3. Continuous-Time Conversion: The logarithmic transformation provides an accurate mapping from discrete to continuous time, essential for physical interpretation.


Limitations

  1. Simplified Stiffness Assumption: Setting K=1K = 1 may not reflect the true system behavior for applications requiring absolute stiffness values.
  2. Dominant Pole Assumption: Selecting a single dominant pole may not accurately capture systems with multiple significant modes.
  3. Low-Frequency Limitation: If the sampling frequency (FsF_s) is low or the poles are near the unit circle, the logarithmic transformation may introduce inaccuracies.


Applications

  1. Biomechanics: Estimating joint or muscle dynamics during motion analysis.
  2. Mechanical Engineering: Identifying parameters in vibrating structures or machines.
  3. Signal Processing: Understanding the dynamic response of a system from observed data.


Conclusion

This function provides a streamlined approach to estimating the physical parameters of a second-order system from time-series data. While effective for systems dominated by a single mode, further refinement might be needed for more complex or higher-order systems. The balance of computational simplicity and physical interpretability makes it a valuable tool for quick system identification tasks.


Here's a comparative analysis of the two explanations (DeepSeek and ChatGPT-4) regarding their strengths, weaknesses, and overall insights:


Comparison


DeepSeek Analysis

Pros:

  • Well-organized and easy to follow.
  • Provides a good overview of the function’s purpose and workflow.
  • Appropriate for readers who need a quick understanding of the function without technical jargon.

Cons:

  • Lacks technical depth and detailed explanations of key mathematical concepts.
  • Does not emphasize error scenarios or discuss potential pitfalls.
  • Applications and limitations are briefly mentioned without specific examples or elaboration.

ChatGPT-4 Analysis

Pros:

  • In-depth and comprehensive, with detailed explanations of the mathematical and computational processes.
  • Highlights potential challenges (e.g., stability validation, dominant pole assumptions).
  • Provides context for broader applications and specific examples in biomechanics and engineering.

Cons:

  • Longer and may be overwhelming for readers seeking a quick summary.
  • Less structured in terms of breaking down individual steps into concise bullet points.
  • May include more detail than necessary for a general audience.


Summary

  • DeepSeek is better for quick, high-level overviews and non-technical audiences.
  • ChatGPT-4 excels in providing a deeper, more technical understanding for users who want to thoroughly analyze the algorithm.


Suggestions for Improvement

  1. DeepSeek could add more technical insights, specific examples, and an explanation of error scenarios to provide a richer analysis.
  2. ChatGPT-4 could streamline its analysis into a more structured format for easier readability while retaining its depth.

This comparison highlights how both analyses complement each other, serving different audiences and purposes effectively.

DeepSeek: Hype or substance? For me, the real value so far is the free ChatGPT access. The ChatGPT o3 mini is another major improvement, and most importantly it's now available! #DeepSeek #ChatGPT #AI #Debate

Like
Reply

It's interesting to compare the responses from DeepSeek and OpenAI. Looking at this particular example, the difference in detail is quite noticeable. DeepSeek's response, while helpful, lacks the depth and comprehensive mathematical explanations that OpenAI provides. This suggests a potential gap in the underlying knowledge base DeepSeek was trained on, particularly when it comes to complex mathematical concepts. It highlights how crucial the training data is for the performance of these models.

Like
Reply

To view or add a comment, sign in

More articles by Lawrence Casha

Others also viewed

Explore content categories