# How to obtain regression polynomial equation with more than 2 independent variables with degree 5.

10 views (last 30 days)

Show older comments

How to obtain regression polynomial equation with more than 2 independent variables with degree 5, because curve fitting tool in MATLAB only support 2 independent variables.

I want the equation with 3 independent variable with coefficient provided, also plot the graph.

Anyone can help me ? Thank you in advance.

##### 2 Comments

David Wilson
on 31 Mar 2020

### Accepted Answer

David Wilson
on 4 Apr 2020

OK, the fitting is easy, the plotting less so. Since you didn't give us too many hints, not did you give us any data to work with, I'll make up my own.

Let's start with a fake 3D equation, where we know the true coefficients, and then try to recover them. I'll use something simple like,

Here you can see that my coefficients are, in the order given above, p = [1, -3, 1, -1]. If we get these back again, we know we are on the right track.

fun3D = @(x,y,z) x.^2 - 3*x.*y.^3 +x.^2.*z - z.^3

Now we will sample this function with 200 values, somewhat randomly.

N = 200; % # of points. I hope you have a lot !

x = rand(N,1); y = randn(N,1); z = rand(N,1)*2-1;

F = fun3D(x,y,z);

stem3(x,y,z) % only shows layout

For the moment, all I have done is given a plot of the independent variables, I haven't yet plotted the 4D function. (That's going to be always tricky, but since you asked for it, I hope you have the relevant 4D goggles!)

Now we are ready to recover the parameters. If I choose (somehow) a perfect model structure, then I should recover the parameters exactly. You may need to revise the maths behind the next two steps.

>> V = [x.^2, x.*y.^3, x.^2.*z, z.^3, ones(size(x))] % Vandemonde matrix

>> p = V\F % do LS fit

p =

1.0000

-3.0000

1.0000

-1.0000

0.0000

We got exactly the parameters what we were looking for. Let's do a plot. For more info, check out the help for isosurface etc.

col = colororder;

xi = linspace(0,1)'; yi = linspace(-2,2)'; zi = linspace(-1,1)';

[X,Y,Z] = meshgrid(xi, yi, zi);

Fi = p(1)*X.^2 - p(2)*X.*Y.^3 +p(3)*X.^2.*Z - p(4)*Z.^3;

fval = [-3:3];

for i=1:7

pa = patch(isosurface(X,Y,Z, Fi, fval(i)));

isonormals(X,Y,Z,Fi, pa)

pa.FaceColor = col(i,:);

pa.EdgeColor = 'none';

end

daspect([1 1 1])

view(3)

camlight; lighting phong

Now I hope that is meaningful for you. Most people wouldn't bother.

In reality, we wouldn't know the exact structure of the 3D function, so we might have to guess something like:

V = [x.^2, x, y.^2 y, z.^2, z, x.*y x.*z, y.*z ones(size(x))]

p = V\F

##### 0 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!