How can i use a defined variable in equation for fitting ?

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)

Seems that using the following is doing the work :
temp = [320; 330; 340; 350];
prop = [427; 452; 503; 537];
b_def = 3;
ub1 = fittype('a * x + b', 'problem', 'b')
ub = fit(temp, prop, ub1, 'problem', b_def)
plot(temp,prop,'ro');
hold on;
plot(ub, temp, prop);
hold off;
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
Is this working for a non linear curve ? I am not trying to solve this specific example but to use a defined variable inside the equation for fitting.

Categories

Asked:

on 13 Dec 2017

Answered:

on 13 Dec 2017

Community Treasure Hunt

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

Start Hunting!