Least Square Linear Approximation
15 次查看(过去 30 天)
显示 更早的评论
Hi,
Based on the least square linear approximation, use this to fit a the function y = aln(x) + bcos(x) + ce^x to the data below. Find a, b, and c.
x=[0.24, 0.65, 0.95, 1.24, 1.73, 2.01, 2.23, 2.52, 2.77, 2.99];
x=x'; %Transposes x%
y=[0.23, -0.26, -1.10, -0.45, 0.27, 0.10, -0.29, 0.24, 0.56, 1.00];
y1=[log(x) cos(x) exp(x)]
When I run this code it gives me a 3x10 matrix, how do I find the value of a, b and c (each one should have one value)?
Thank you so much in advance!
0 个评论
采纳的回答
Star Strider
2017-2-11
You’re almost there! Please let me introduce you to the mldivide,\ (link) function and operator to get you the rest of the way:
The Code —
x=[0.24, 0.65, 0.95, 1.24, 1.73, 2.01, 2.23, 2.52, 2.77, 2.99];
y=[0.23, -0.26, -1.10, -0.45, 0.27, 0.10, -0.29, 0.24, 0.56, 1.00];
DesignMatrix = [log(x(:)) cos(x(:)) exp(x(:))];
Coefficients = DesignMatrix\y(:);
a = Coefficients(1)
b = Coefficients(2)
c = Coefficients(3)
The Results —
a =
-1.04103221690367
b =
-1.26131878469978
c =
0.030734825739463
The colon index reference (:) creates column vectors out of whatever they reference, from vectors to multi-dimensional matrices. That also eliminates ambiguities such as having to know if a vector is a row or column vector before transposing it.
2 个评论
Star Strider
2017-2-11
My pleasure!
I was ‘introduced’ to MATLAB (dragging me away kicking and screaming from FORTRAN that I had been programming in since I learned it as an undergraduate 25 years earlier) in a graduate school control systems continuing education course in the early 1990s.
I became as proficient in MATLAB as I am (and there are likely some here who would question that ‘proficient’ includes me) by hanging out here on MATLAB Answers, on and off for the past five years. The standards here are very high, there are many here with expertise in computational applied maths, statistics, and other disciplines that had me going back to the books on several occasions to learn more about those topics. (In that respect, being a Board Certified Internal Medicine Physician and Biomedical Engineer, I bring my own areas of expertise here.) MATLAB Answers is ‘intellectually competitive’ in ideally the best sense of that idea, with better ideas and approaches emerging constantly. (I believe I have contributed a few original ideas myself.) It is only possible not to learn better programming, statistics, data interpretation and other necessary techniques here by remaining oblivious to what others contribute.
It’s comforting to realise that none of us were born proficient in MATLAB. We’re all learning.
My one suggestion is that you hang out here, read the Questions and Answers, Answer Questions you feel comfortable with, and as you learn more about MATLAB and what constitute good Answers, you will earn Reputation Points (recognition by others here of the quality of your Answers and Comments), gain confidence, and add your own areas of expertise. If you have ideas for File Exchange contributions from your own experience (almost all of mine began as problems I had to solve for my own research), code them and contribute them as well.
You’re always welcome here!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Algebra 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!