How do i do a two dimensional linear regression fit

조회 수: 15 (최근 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 CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by