Write an expFit function that calls linefit to fit data to y = C1*e^(C2*x)

3 Ansichten (letzte 30 Tage)
Ryan
Ryan am 18 Dez. 2014
Kommentiert: Ryan am 19 Dez. 2014
Hey guys, I'm not quite sure where to start with this one. I was hoping to find some guidance as to what direction I need to take in order to get the desired result, and also what the whole concept is behind this. ANY help is highly valued so PLEASE feel free to respond with anything you got. Time is of the essence in the engineering world.
I saw the thread that contained the same question that I am asking but not much was answered in that thread so I thought I would make one that tailored to my specific questions and parameters.
This problem is based on converting non-linear relationships into linear ones, so that linear regression methods can be applied for fitting.
The question states, "Write an expFit function that calls linefit to fit data to y = C1*e^(C2*x)" Then it gives us the following data in order to test our function...
x = [1, 2.5, 4, 5.5, 7] y = [2.285, 1.417, 0.879, 0.545, 0.338]
I hope this can be a learning experience for me as I really want to improve in this area of coding, so please share any knowledge or concepts related to this topic.
  12 Kommentare
John D'Errico
John D'Errico am 19 Dez. 2014
Bearbeitet: John D'Errico am 19 Dez. 2014
Your initial quote reads...
"Write an expFit function that calls linefit to fit data to y = C1*e^(C2*x)"
My guess is that at some point, you were either supplied with a function called linefit, OR that in an earlier assignment, you were told to write a function by that name, to fit a straight line to data. Written as it was, that seems to imply that something called linefit is presumed to exist. Since MATLAB does not provide a tool called linefit with its general distribution, that further implies you were either given the code, or presumed to have written it.
What you have so far is a script, i.e., a string of MATLAB commands that can be executed (essentially in the command window) in sequence to do some operation.
The quote tells you to write a function though. A function allows you to pass in arguments to the function, where it will work on them as data. The nice thing about a function is it allows you to then use it in the future to work on other sets of data.
As a function, you would pass in as arguments x and y, and it would return perhaps the parameters of the exponential fit you have just done. The function might also generate a plot, or do a variety of things.
For example, polyfit is itself a function, already provided to you in MATLAB. That you can write your own functions to enhance the capabilities of MATLAB for your own specific purposes is a very important feature of such a language.
Ryan
Ryan am 19 Dez. 2014
I think I got it! Here's the code that I have so far. It takes user input data and solves for the C1 and C2 of y = C2*e^(C2*x). And you were correct.. My professor just emailed me explaining that "expFit" is not an existing function. It is the name/use of the function that I am writing in order to call on "lineFit", a function that was supposedly previously given to us during the semester.
clc
clear
disp('Function: y = C1*e^(C2*x)')
fit_x = input('X Array of Data: ');
fit_y = input('Y Array of Data: ');
if length(fit_y)~= length(fit_x), error('x and y do not have equal dimensions.'); end
coef = polyfit(fit_x,log(fit_y),1);
C2 = coef(1);
D1 = coef(2);
C1 = exp(D1);
disp('Constant 1(C1): ')
disp(C1)
disp('Constant 2(C2): ')
disp(C2)
x = [1:1.5:7];
y = C1*exp(C2*x);
plot(fit_x,fit_y,'ro',x,y)
grid on
xlabel 'X'
ylabel 'Y'

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by