我有下面描述的x向量和y向量:
x = [0 5 8 15 18 25 30 38 42 45 50];
y = [81.94 75.94 70.06 60.94 57.00 50.83 46.83 42.83 40.94 39.00 38.06];
使用这些值如何找到y = a*(b^x)
??
我已经尝试过此代码,但它找到了y = a*e^(b*x)
clear, clc, close all, format compact, format long
% enter data
x = [0 5 8 15 18 25 30 38];
y = [81.94 75.94 70.06 60.94 57.00 50.83 46.83 42.83];
n = length(x);
y2 = log(y);
j = sum(x);
k = sum(y2);
l = sum(x.^2);
m = sum(y2.^2);
r2 = sum(x .* y2);
b = (n * r2 - k * j) / (n * l - j^2)
a = exp((k-b*j)/n)
y = a * exp(b * 35)
result_68 = log(68/a)/b
我知道插值技术,但我无法将其实现到我的解决方案......
非常感谢提前!
答案 0 :(得分:4)
由于 y = a * b ^ x 与log( y <您可以执行/ em>)= log( a )+ x 日志( b )
>> y = y(:);
>> x = x(:);
>> logy = log(y);
>> beta = regress(logy, [ones(size(x)), x]);
>> loga = beta(1);
>> logb = beta(2);
>> a = exp(loga);
>> b = exp(logb);
因此 a 和 b 的值为
>> a, b
a =
78.8627588780382
b =
0.984328823937827
绘制拟合
>> plot(x, y, '.', x, a * b .^ x, '-')
给你这个 -
NB regress
函数来自统计工具箱,但您可以定义一个非常简单的版本,它可以满足您的需求
function beta = regress(y, x)
beta = (x' * x) \ (x' * y);
end
答案 1 :(得分:1)
作为Chris Taylor给出的答案的扩展,它提供了对数变换域中的最佳线性拟合,通过直接求解非线性问题,您可以找到更好的拟合在原始域中,例如, here
使用例如Chris给出的解决方案作为起点:
x = [0 5 8 15 18 25 30 38 42 45 50];
y = [81.94 75.94 70.06 60.94 57.00 50.83 46.83 42.83 40.94 39.00 38.06];
regress = @(y, x) (x' * x) \ (x' * y);
y = y(:);
x = x(:);
logy = log(y);
beta = regress(logy, [ones(size(x)), x]);
loga = beta(1);
logb = beta(2);
a = exp(loga)
b = exp(logb)
error = sum((a*b.^x - y).^2)
给出了:
>> a, b, error
a =
78.862758878038164
b =
0.984328823937827
error =
42.275290442577422
您可以进一步迭代以找到更好的解决方案
beta = [a; b];
iter = 20
for k = 1:iter
fi = beta(1)*beta(2).^x;
ri = y - fi;
J = [ beta(2).^x, beta(1)*beta(2).^(x-1).*x ]';
JJ = J * J';
Jr = J * ri;
delta = JJ \ Jr;
beta = beta + delta;
end
a = beta(1)
b = beta(2)
error = sum((a*b.^x - y).^2)
,并提供:
>> a, b, error
a =
80.332725222265623
b =
0.983480686478288
error =
35.978195088265906
答案 2 :(得分:0)
还有一个选择是使用MATLAB的Curve Fitting Toolbox,它允许您以交互方式生成拟合,并且还可以发出运行拟合所需的MATLAB代码。