Reputation: 874
I am trying to model some measures (lux, ohm)
that behave as a logarithmic function.
In order to do it, I've tried to model it with MATLAB by projecting the real values using natural logarithms, then use polyfit
to get a linear expression. Then, I want to isolate the variable lux
.
What I have so far is:
% Original values from spreadsheet
lux = [1, 5, 10, 50, 100]';
ohm = [100, 35, 25, 8, 6]';
% Logaritmic fitting
x_ = log(lux);
y_ = log(ohm);
p = polyfit(x_,y_,1)
x1 = linspace(x_(1), x_(end), 1000);
y1 = polyval(p,x1);
plot(x_,y_,'ob');
hold on;
plot(x1,y1, 'r');
hold on;
% Get expression
% y = -0.6212x + 4.5944
% log(ohm) = -0.6212 * log(lux) + 4.5944
lfun = @(ohm, a, b) ((ohm/exp(b)).^(1/a));
a = p(1);
b = p(2);
t = linspace(1,100,100);
plot(log(t), log(lfun(t, a, b)), '--g');
hold off;
legend({'points','poly','eq'});
Since I got p = -0.6212 4.5944
, I assume that the equation is log(ohm) = -0.6212 * log(lux) + 4.5944
. Isolating the variable lux
results in:
lux = (ohm / exp(4.5944) ).^(-1/0.6212)
However, as can be seen in the green line, it is not working!
What am I doing wrong?
Upvotes: 2
Views: 810
Reputation: 11628
You're doing everything right except for the plotting, in the first plot you defined the x-axis to be log(lux)
and the y-axis to be log(ohm)
, but to adhere to that in the second case you need to flip the arguments:
plot(log(lfun(t, a, b)), log(t), '--g')
t
refers to 'ohm' and must therefore be displayed on the y-axis to coincide with the first plot.
Upvotes: 2