# Curve fitting for loglog scale

9 views (last 30 days)
Commented: dpb on 10 May 2022
I have fitted a curve using general plot function, now if I want to fit the same data in loglog scale how can I do it.
any help is much appreciated
program:
function [fitresult, gof] = createFit(w, AC1)
%CREATEFIT(W,AC)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : w
% Y Output: AC
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% Auto-generated by MATLAB on 10-May-2022 11:38:11
%% Fit: 'untitled fit 1'.
clc;clear,close all;
% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "A2:F202";
% Specify column names and types
opts.VariableNames = ["f1", "sigmaAcZT1", "f2", "SIGMAACZT2", "F2", "SIGMAACZT3"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double"];
% Import the data
% Convert to output type
data = table2array(data);
% Clear temporary variables
clear opts
f = data(:,1)
w = 2*pi*data(:,1);%w = 2*pi*f
AC1 = data(:,2);%AC conductivity sample 1
AC2 = data(:,4);%AC conductivity sample 2
AC3 = data(:,6);%AC conductivity sample 3
[xData1, yData1] = prepareCurveData( w, AC1 );
[xData2, yData2] = prepareCurveData( w, AC2 );
[xData3, yData3] = prepareCurveData( w, AC3 );
% Set up fittype and options.
ft = fittype( 'power2' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.StartPoint = [9.32271357188677e-06 0.0429375187609614 2.50935751249309e-06];
% Fit model to data.
[fitresult1, gof1] = fit( xData1, yData1, ft, opts );
[fitresult2, gof2] = fit( xData2, yData2, ft, opts );
[fitresult3, gof3] = fit( xData3, yData3, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot(fitresult1,'k',xData1,yData1,'ok');
hold on
g = plot(fitresult2,'k',xData2,yData2,'or');
hold on
f = plot(fitresult3,'k',xData3,yData3,'ob');
legend('ZT-3','fit', 'Location', 'NorthWest')
set(h, 'LineWidth',2)
set(g, 'LineWidth',2)
set(f, 'LineWidth',2)
% Label axes
set(gca,'fontweight','bold','fontsize',16);
xlabel( '\omega');
ylabel('\sigma_{AC} (S/cm)');
fitresult1
fitresult2
fitresult3
grid on
dpb on 10 May 2022
I'll disagree w/ @Torsten that there's never a valid reason for log models; indeed some processes are. The error distribution is mutiplicative rather than additive in this case
As said above, you have two choices -- either
1. log the variables, fit, evaluate y=10.^f(log10X), loglog(X,Y) (because it does the log10 internally), or
2. use nonlinear curve fit and fit the appropriate power model directly. In this case your model will still be in original space; loglog() works as normal for both data and fitted result.

### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!