How can I improve the attached code to reduce the gap between fitted curve and the original ones?
3 views (last 30 days)
Show older comments
Hello friends
I have a code that I want to know the amplitude of Vabc between t=0.1 and 0.2
But the fitted curve is a bit different from the original ones.
Can you advise?
3 Comments
Answers (1)
Sandeep Mishra
on 3 Dec 2024
Hi Atefeh,
I ran the provided code in MATLAB R2024b and identified the same disparity between the curve plotted using the data and the curve obtained through curve fitting.
Upon debugging, I noticed that the code uses the ‘fit’ function across three time ranges: ‘ta’, ‘tb’, and ‘tc’. The RMSE for the ‘tb’ range is approximately 0.2, whereas for ‘ta’ and ‘tc’, it is significantly lower at 1e-6.
The discrepancy arises due to the high RMSE for the ‘tb’ time range. This occurs because the ‘fit’ function applies the ‘sin1’ ‘fittype’ parameter to data comprising a linear segment (from 0.1s to 0.2s) followed by a sine wave (from 0.2s to 0.21s), leading to inconsistent results.
To resolve the issue, you can update the time range of ‘tb’ and ‘tc’ interval to focus on a single curve type at a time.
Below is an example modification:
% Split data for straight line
tb = Vtime(1002:2000);
Vb = Vdat1(1002:2000);
% Split data for sine wave
tc = Vtime(2001:3001);
Vc = Vdat1(2001:3001);
% Updated time interval for curve fitting
t = linspace(0, 0.3, 3001);
t1 = 0.1;
t2 = 0.2;
Refer to the following MathWorks Documentation to learn about ‘fit’ function:
I hope this helps you in resolving the query
0 Comments
See Also
Categories
Find more on Least Squares 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!