How do i do a two dimensional linear regression fit

17 ビュー (過去 30 日間)
jdm_dm
jdm_dm 2012 年 7 月 24 日
回答済み: Bahloul Derradji 2020 年 7 月 2 日
Hey,
I'm working with winddata and for a model i need to do a two-dimensional linear regression fit of the form
[y1;y2]=[a1;a2]+[b1,b2;b3,b4]*[x1;x2]
(x1,X2) and (y1,y2) are know and i want to determine to a and b coefficients.
Can anybody help me with this? The x en y coordinates are both 4392x2.
Thanks in advance!

回答 (3 件)

the cyclist
the cyclist 2012 年 7 月 24 日
Do you have the Statistics Toolbox? If so, I believe you can use the mvregress() function to do this.
  2 件のコメント
jdm_dm
jdm_dm 2012 年 7 月 24 日
Hey,
Thanks for the quick response! But it doesn't seems to work. I've put the x and y coordinates in separate arrays respectively X and Y (both size 4392x2). When i do p=mvregress(Y,X)I get an error message: X must be a cell array if Y has multiple columns.
When I try to put X in a cell array (I call him mycell) and then try p=mvregress(Y,mycell) I get the following error: Undefined function 'isnan' for input arguments of type 'cell'.
Thanks,
Jdm
the cyclist
the cyclist 2012 年 7 月 24 日
編集済み: the cyclist 2012 年 7 月 24 日
I suggest you look at the "flu" example here:
to help you debug your syntax.

サインインしてコメントする。


Image Analyst
Image Analyst 2012 年 7 月 24 日

Bahloul Derradji
Bahloul Derradji 2020 年 7 月 2 日
use the following ready to use example code:
this is the mainfile.m
clear all
clc
clf
close all
xdata =( -1:0.1:+1);
ydata=(-1:0.2:1)';
nx=numel(xdata);
ny=numel(ydata);
ax=0.8;
ay=0.4;
A=5;
[X,Y]= meshgrid(xdata,ydata);
Z=A*exp(-((X/ax).^2+(Y/ay).^2))+ 0.05*rand(ny,nx);
surf(X,Y,Z);
s = surf(X,Y,Z,'FaceAlpha',0.4);
s.EdgeColor = 'none';
s.FaceColor = 'red';
xlabel('x')
ylabel('y')
zlabel('z')
x = reshape(X,[],1);
y = reshape(Y,[],1);
z = reshape(Z,[],1);
%cftool
[fitresult, gof] = createFit(x, y, z);
disp(fitresult)
Here is the createFit.m script;
function [fitresult, gof] = createFit(x, y, z)
%% Fit: 'Gaussian fit 1'.
[xData, yData, zData] = prepareSurfaceData( x, y, z );
% Set up fittype and options.
ft = fittype( 'a*exp(-(x/wx)^2-(y/wy)^2)', 'independent', {'x', 'y'}, 'dependent', 'z' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.890036233228213 0.330202242514021 0.22970119787112];
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
% Plot fit with data.
figure( 'Name', 'Gaussian fit 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, 'Gaussian fit 1', 'z vs. x, y', 'Location', 'NorthEast' );
% Label axes
xlabel x
ylabel y
zlabel z
grid on
% enjoy.

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by