This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Multivariate Regression

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

The stats::reg function also performs linear and nonlinear regressions with two or more independent variables. The following example demonstrates how to perform regression and fit the data to a function of two variables. Suppose, you have three data lists that contain coordinates x, y, and z of discrete points:

sampleX := [
   -0.9612553839, -0.329576986, 0.7544749248, 0.7339191669,
   -0.294101483, -0.9809519422, -0.6251624775, -0.1885706545,
    0.4729466504, 0.4402179092, -0.1883574567, -0.6260246367,
   -0.0274947885, -0.01843922645, -0.02687538212, -0.03682895886,
   -0.009212115975, -0.04956242636]:
sampleY := [
   -0.02185415496, -0.9146217269, -0.5792023459, 0.5440822742,
    0.8848317212, -0.03925037966, -0.02360776024, -0.5657632266,
   -0.3461422332, 0.3429495709, 0.5113552882, -0.02089004809,
   -0.03700165982, -0.0226531849, -0.004897297126, -0.03063832565,
   -0.03469956571, -0.01391540741]:
sampleZ := [
    0.2755344332, 0.272077192, 0.2682296712, 0.2915713541,
    0.2737466882, 0.3060314064, 0.7624231851, 0.8013891042,
    0.7755723041, 0.7631156115, 0.7816602999, 0.7807856826,
    0.9679031724, 0.9661527172, 0.9632260164, 0.986479402,
    0.9554368723, 0.9768285979]:

Suppose, you want to find the surface that fits these points. Start with plotting the data. The plot::PointList3d function, which plots a finite number of discrete points, requires the coordinates of each point to be grouped together. The following commands create separate lists for the coordinates of each point: [xi, yi, zi] and put these lists into one nested list:

points := [[sampleX[i], sampleY[i], sampleZ[i]]
                        $ i = 1..nops(sampleX)]:

Now, use the plot::PointList3d function to visualize the data:

plot1 := plot::PointList3d(points, PointSize = 2.5):

Rotating the plot can help you guess which surface can possibly fit the data. This plot shows that the data can belong to the upper half of a sphere. Thus, try to fit the data to a sphere with an unknown radius r. The stats::reg function searches for the best fitting value for the parameter r:

fit := stats::reg(sampleX, sampleY, sampleZ,
     sqrt(r^2 - x1^2 - y1^2), [x1, y1], [r])

The stats::reg function also accepts the nested list points:

fit := stats::reg(points, sqrt(r^2 - x1^2 - y1^2),
                                    [x1, y1], [r])

The stats::reg function returns the parameters of the model and the quadratic error in a form of a nested list. To access the parameters separately, use the following commands:

R := op(fit)[1][1];
chi2 := op(fit)[2]

Now, plot the data and sphere that you used to fit the data on the same plot. Rotate the plot to see how the sphere fits the data:

plot2 := plot::Function3d(sqrt(R^2 - x^2 - y^2),
                          x = -1..1, y = -1..1):
plot(plot1, plot2)

Was this topic helpful?