MATLAB - Plot multiple surface fits in one figure - matlab

I have 3 sets of 3D co-ordinates and I have fitted planes to each set. Now, I want to plot all the data points and the 3 planes in one figure.
So far, I have the following function:
function [fitresult, gof] = create_fit(xx, yy, zz, grp)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
% figure( 'Name', 'fit1' );
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
if grp == 1
colormap(hot);
elseif grp == 2
colormap(cool);
else
colormap(grey);
end
legend( h, 'fit1', 'zz vs. xx, yy', 'Location', 'NorthEast' );
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
grid on
However, there are 2 problems with this:
The planes, when plotted individually are scaled according to the data points which are also plotted with them. When the planes are plotted together, they scale badly and the data points are converge to a small blob (very small scale compared to the planes)
I tried fixing the problem with axis([-0.04 0.04 -0.04 0.04 -0.04 0.04 -1 1]);, but it is hard coded and still looks a little off.
The colormap command seems to work only when called for the first time. Hence, all the planes turn out to be blue. How can I color each plane and the points fitted for that plane differently?
Is this the best way to plot multiple planes?

Edit
Here is an edited version of my initial answer. The output of plot is a two-element graphical object, so you have to call separately h(1) and h(2) to set the properties of the plane and of the data points.
Here is the code for the function:
function [fitresult, gof, h] = create_fit(xx, yy, zz, color)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
set(h(1), 'FaceColor', color);
set(h(2), 'MarkerFaceColor', color, 'MarkerEdgeColor', 'k');
and here is a sample script that calls the function:
% Define sample data
N = 20;
x = rand(1,N);
y = rand(1,N);
z = rand(1,N);
% Call the function, specify color
[f1, gof1, h1] = create_fit(x, y, z, 'r');
[f2, gof2, h2] = create_fit(x, y.^10, z, 'g');
[f3, gof3, h3] = create_fit(x.^10, y, z, 'b');
% Figure adjustments
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
view(3)
grid on
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
zlim([min(z) max(z)]);
and the result:

Related

how to change Matlab Curve fitting opts.StartPoint to a range of number using for loop

I am trying to use a different starting point for my custom equation in the MatLab curve fitting tool.
I was able to define a fix starting point for my parameters a,b,c, and o.
i.e. opts.StartPoint = [25.364 26.01 1.02 4.6]
I want to do a for loop that can try 10 different starting points until reaching the original starting point [25.364 26.01 1.02 4.6].
This is what I did so far, and my data is just a table with x and y.
x = age of pavement that goes from 1 to 20 and
y = pavement roughness data that goes from 2.4 to 4.6
thank you for your help, this is my code below:
function [fitresult, gof] = FitTestPSI(x, y)
S = uiimport('-file');
x = S.data(:,1);
y = S.data(:,2);
[xData, yData] = prepareCurveData( x, y );
ft = fittype( 'o-exp(a-b*c^log(1/x))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.StartPoint = [25.364 26.01 1.02 4.6];
[fitresult, gof] = fit( xData, yData, ft, opts );
disp(gof.sse);
disp(gof.rmse);
figure( 'Name', 'PSI' );
h = plot( fitresult, xData, yData );
legend( h, 'Measured', 'Estimated', 'Location', 'NorthEast');
text(15,3.2,sprintf('SSE = %g\nRMSE = %g',gof.sse,gof.rmse))
xlabel AGE(year)
ylabel PSI
title('Orig. BIT Constr.')
grid on
end

MATLAB Colorbar - Same colors, scaled values

Consider the following example:
[ X, Y, Z ] = peaks( 30 );
figure( 100 );
surfc( X, Y, Z );
zlabel( 'Absolute Values' );
colormap jet;
c = colorbar( 'Location', 'EastOutside' );
ylabel( c, 'Relative Values' );
The output looks as follows:
How can I scale the ticks on the colorbar, i.e. scale the c-axis (e.g. divide the values by 100):
without changing the z values and colors on the plot
without changing the colors on the colorbar
without changing the relation between the colors on the plot, the colors on the colorbar and the z values of the plot
while still using the full range of the colorbar
In the picture above, I would like to scale the c-axis such that it shows this values for the related z:
z | c-axis
----------
8 | 8/100
6 | 6/100
4 | 4/100
. | ...
The function caxis, as I understand it, is not suited here, as it would just show the colors for a subsection of the z-axis and not for the whole z-axis.
Bonus question: How could one scale the color mapping and the colorbar as a function of X, Y and/or Z?
[ X, Y, Z ] = peaks( 30 );
figure( 101 );
surfc( X, Y, Z );
zlabel( 'Absolute Values' );
%
Z_Scl = 0.01;
Z_Bar = linspace( min(Z(:)), max(Z(:)), 10 );
%
colormap jet;
c = colorbar( 'Location', 'EastOutside', ...
'Ticks', Z_Bar, 'TickLabels', cellstr( num2str( Z_Bar(:)*Z_Scl, '%.3e' ) ) );
ylabel( c, 'Relative Values' );
For an arbitrary mapping between the z-values and the colorbar, it is possible to combine surf, contourf and contour as follows (inspired by these two great answers):
[ X, Y, Z ] = peaks( 30 ); % Generate data
CB_Z = (sin( Z/max(Z(:)) ) - cos( Z/max(Z(:)) )).^2 + X/5 - Y/7; % Generate colormap
CF_Z = min( Z(:) ); % Calculate offset for filled contour plot
CR_Z = max( Z(:) ); % Calculate offset for contour plot
%
figure( 102 ); % Create figure
clf( 102 );
hold on; grid on; grid minor; % Retain current plot and create grid
xlabel( 'x' ); % Create label for x-axis
ylabel( 'y' ); % Create label for y-axis
zlabel( 'Scaling 1' ); % Create label for z-axis
surf( X, Y, Z, CB_Z ); % Create surface plot
%
CF_H = hgtransform( 'Parent', gca ); % https://stackoverflow.com/a/24624311/8288778
contourf( X, Y, CB_Z, 20, 'Parent', CF_H ); % Create filled contour plot
set( CF_H, 'Matrix', makehgtform( 'translate', [ 0, 0, CF_Z ] ) ); % https://stackoverflow.com/a/24624311/8288778
%
CR_H = hgtransform( 'Parent', gca ); % https://stackoverflow.com/a/24624311/8288778
contour( X, Y, CB_Z, 20, 'Parent', CR_H ); % Create contour plot
set( CR_H, 'Matrix', makehgtform( 'translate', [ 0, 0, CR_Z ] ) ); % https://stackoverflow.com/a/24624311/8288778
%
colormap jet; % Set current colormap
CB_H = colorbar( 'Location', 'EastOutside' ); % Create colorbar
caxis( [ min( CB_Z(:) ), max( CB_Z(:) ) ] ); % Set the color limits for the colorbar
ylabel( CB_H, 'Scaling 2' ); % Create label for colorbar
As I wrote in your answer, I think a better choice for showing two related values is not to create a new axis for that, but to show them one near the other. Here is a suggestion:
[X,Y,Z] = peaks(30);
surfc(X,Y,Z);
zlabel('Absolute (Relative) Values');
colormap jet
Z_Scl = 0.01;
zticks = get(gca,'ZTick');
set(gca,'ZTickLabel',sprintf('%g (%g)\n',[zticks;zticks.*Z_Scl]))

Size an style of data points in cftool

Is there any way to control size, color and style of the black data points in the figure below?
Image
Code
%% Fit: 'untitled fit 1'.
[xData, yData, zData] = prepareSurfaceData( x_lim, y_lim, z_lim );
% Set up fittype and options.
ft = 'linearinterp';
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, 'untitled fit 1', 'z_lim vs. x_lim, y_lim', 'Location', 'NorthEast' );
% Label axes
xlabel( 'x_lim' );
ylabel( 'y_lim' );
zlabel( 'z_lim' );
grid on
view( -253.5, 42.0 );
From your code, the handle returned when you plot, h should be of length 2. h(1) has the details for the surface, and h(2) for the points. Therefore, you can use set on various properties of this handle:
set(h(2),'Marker','o');
(Other properties you may want to set: MarkerSize, MarkerEdgeColor, MarkerFaceColor).

fitting curve with nonlinear function using cftool in matlab

I need to fit a data with a nonlinear function just like this:
y=a+b*cos(c*x+d)+e*exp(f*x);
where a, b, c, d, e and f are the coefficients to be found. I have used the Custom Equation method in cftool, the result is bad, then how to adjust the StartPoint in cftool to get a better result? Otherwise, there are other ways to solve my problem? Thank so much.
Here is my data, each in 1*35:
x = [1970:2004];
y = [46808,49416,53094,57237,56677,56198,59673,61826,64158,65220,63108,60944,59543,58779,59882,60087,61825,63104,64963,66902,66443,67061,67273,67372,68679,69995,71522,73292,73932,75826,76954,78105,78439,79892,82631];
ok, Dan, here is my code about cftool, the function is generated by Custom Equation of cftool
% This file include two functions:
% 1. Main: testForFit.m
% 2. Function generated by cftool
function testForFit
clc; clear all;
x = 1970:2004;
y = [46808,49416,53094,57237,56677,56198,59673,61826,64158,65220,63108,60944,59543,58779,59882,60087,61825,63104,64963,66902,66443,67061,67273,67372,68679,69995,71522,73292,73932,75826,76954,78105,78439,79892,82631];
plot(x, y, 'or')
[fitresult, gof] = createFit(x, y)
end
function [fitresult, gof] = createFit(x, y)
%CREATEFIT(X,Y)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : x
% Y Output: y
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 04-Feb-2014 00:23:03
%% Fit: curve fitting of gived form of function.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'a+b*cos(c*x+d)+e*exp(f*x)', 'independent', 'x', 'dependent', 'y' );
% ft = fittype( 'a+e*exp(f*x)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
% http://stackoverflow.com/questions/21551951/fitting-curve-with-nonlinear-function-using-cftool-in-matlab
% Firstly, give the estimation, then calculate it
% funlist={1, #(p,x) cos(p(1)*x+p(2)), #(p,x) exp(p(3)*x)};
% NLPstart = [0.605 -3159.659 0.02369];
% [cdf,abe] = fminspleas(funlist,NLPstart,x,y)
% c=cdf(1); d=cdf(2); f=cdf(3);
% a=abe(1); b=abe(2); e=abe(3);
% opts.StartPoint = [a b c d e f];
% How to give the value of StartPoint ?
opts.StartPoint = [0.957166948242946 0.485375648722841 0.8002804688888 0.141886338627215 0.421761282626275 0.915735525189067];
% opts.StartPoint = [31950 1557 0.605 -3159.659 1.183e-16 0.02369];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'curve fitting' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x', 'curve fitting', 'Location', 'NorthEast' );
% Label axes
% 31950 1557 0.605 -3159.659 1.183e-16 0.02369
% title('31950+1557*cos(0.605*x-3159.659)+1.183e-16*exp(0.02369*x)')
xlabel( 'x' );
ylabel( 'y' );
grid on
% x = [1970:2004];
% y = [46808,49416,53094,57237,56677,56198,59673,61826,64158,65220,63108,60944,59543,58779,59882,60087,61825,63104,64963,66902,66443,67061,67273,67372,68679,69995,71522,73292,73932,75826,76954,78105,78439,79892,82631];
% Baidu Zhidao
% http://zhidao.baidu.com/question/198731054514100005.html?push=keyword
end
Some error will occured if the StartPoint is not proper ...
I don't have the CurveFit Toolbox, but supposedly there is a "Fit Options" button near where you select the curve type, leading to a dialog where you can select your own initial point.
I also recommend that you try FMINSPLEAS, which can take advantage of the fact that y has a linear dependence on 3 of your 6 parameters. You also only need to provide an initial guess for the nonlinear parameters NLPstart=[c,d,f]
funlist={1, #(p,x) cos(p(1)*x+p(2)), #(p,x) exp(p(3)*x)};
[cdf,abe] = fminspleas(funlist,NLPstart,x,y);
c=cdf(1); d=cdf(2); f=cdf(3);
a=abe(1); b=abe(2); e=abe(3);

Matlab curve fitting tool, cftool, generate code function does not give the same fit

I am using Matlab's curve fitting tool, cftool, to fit a set of points which I have. The problem I am facing is that the generate code function will not give me the same fit as produced in the cftool.
This is not what I want because I want to be able to retrieve the data from the residual plot. I could also just copy the function from cftool and do it manually. But I do not understand why the generated code will not just give me the same curve fit.
The cftool session file: http://dl.dropbox.com/u/20782274/test.sfit
The generated code from Matlab:
function [fitresult, gof] = createFit1(Velocity, kWhPerkm)
%CREATEFIT1(VELOCITY,KWHPERKM)
% Create a fit.
%
% Data for 'untitled fit 3' fit:
% X Input : Velocity
% Y Output: kWhPerkm
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 02-Dec-2012 16:36:19
%% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData( Velocity, kWhPerkm );
% Set up fittype and options.
ft = fittype( 'a/(0.008*x) + c*x^2 + d*90', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( ft );
opts.DiffMaxChange = 0.01;
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf];
opts.MaxFunEvals = 1000;
opts.MaxIter = 1000;
opts.StartPoint = [0 0 0];
opts.Upper = [Inf Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Create a figure for the plots.
figure( 'Name', 'untitled fit 3' );
% Plot fit with data.
subplot( 2, 1, 1 );
plot( fitresult, xData, yData, 'predobs' );
% Label axes
xlabel( 'Velocity' );
ylabel( 'kWhPerkm' );
grid on
% Plot residuals.
subplot( 2, 1, 2 );
plot( fitresult, xData, yData, 'residuals' );
% Label axes
xlabel( 'Velocity' );
ylabel( 'kWhPerkm' );
grid on
The curve I get with the generated code:
http://i.stack.imgur.com/65d1P.jpg
The curve I need:
http://i.stack.imgur.com/p3Egp.jpg
So does anyone know what goes wrong?
-edit-
And the Velocity and WhPerkm data file: http://dl.dropbox.com/u/20782274/data.mat
RE: I want to be able to retrieve the data from the residual plot
One way to do this is:
Select "Save to Workspace..." from the Fit menu
Ensure that "Save fit output to MATLAB struct named" is checked.
Note the name of variable. By default, it is output.
Click "OK" to send data to the MATLAB workspace.
In the MATLAB workspace, the residuals will be in output.residuals. For your example, you can plot the residuals via, e.g.,
>> plot( Velocity, output.residuals, '.' )