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).
Recommended by LinkedIn
Key Steps in the Code
1. Initialization
2. Remove DC Offset
3. AR(2) Model Fitting
4. Polynomial Coefficients and Pole Extraction
5. Dominant Pole Selection
6. Discrete-to-Continuous Conversion
7. Natural Frequency (ωn\omega_n) and Damping Ratio (ζ\zeta)
8. System Parameter Estimation
9. Error Handling
Strengths
Limitations
Applications
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:
Cons:
ChatGPT-4 Analysis
Pros:
Cons:
Summary
Suggestions for Improvement
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
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.