How can i use a defined variable in equation for fitting ?
Show older comments
Hi, I have defined previous variable by calculation in matlab, and I want to use the result store in variables inside the equation use for fitting. Even by defining these variables as global, the fitting operation does not take in account the real value of these variable. For example :
temp = [320; 330; 340; 350];
prop = [427; 452; 503; 537];
b = 3;
ub = fit(temp, prop, 'a * x + b')
plot(temp,prop,'ro');
hold on;
plot(ub, temp, prop);
hold off;
this is giving me the following fit :
General model:
ub(x) = a * x + b
Coefficients (with 95% confidence bounds):
a = 3.81 (2.366, 5.254)
b = -796.6 (-1281, -312.4)
But I want "b" to be as defined previously : 3.
How can I use a defined variables inside the equation for fitting ?
Thanks.
Answers (3)
Olivier JAY
on 13 Dec 2017
Star Strider
on 13 Dec 2017
One option is to use an anonymous function:
b = 3;
f = @(a) a.*x + b;
To do a simple linear fit, you need only core MATLAB:
temp = [320; 330; 340; 350];
prop = [427; 452; 503; 537];
b = 3;
b_fit = temp\(prop - b) % (y - b) = a*x
figure(1)
plot(temp, prop, 'pg')
hold on
plot(temp, b_fit*temp, '-r')
hold off
grid
Olivier JAY
on 13 Dec 2017
0 votes
Categories
Find more on Get Started with Curve Fitting Toolbox 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!