# Regress in Matlab

1. Apr 8, 2012

### roam

The following is part of an experiment about the speed of sound in different depths of a liquid, I'm trying to solve it in Matlab.

http://img546.imageshack.us/img546/5952/20120408201206.jpg [Broken]

Here is my attempt:

Code (Text):
clc
clf

N=13;

d=1900:-100:700;

t=[2.62 2.37 2.35 2.25 2.09 1.86 1.78 1.64 1.53 1.35 1.18 1.09 0.89];

plot(d,t, '-m*');

p = polyfit(d,t,1);
hold on
plot(d,polyval(p,d))

tf=polyval(p,d);

for i = 1:1:N

sigmat=sqrt(sum(((t-tf).^2)/(N-2)));

end

[p,sp]=regress(d,t,sigmat,1)
But I keep getting the following error:

Code (Text):
Error using regress
Too many input arguments.

Error in Q2 (line 24)
[p,sp]=regress(d,t,sigmat,1)
What is wrong with my code, and how can I fix it? Any help is greatly appreciated.

Last edited by a moderator: May 5, 2017
2. Apr 8, 2012

### Pythagorean

try typing 'help regress' at the command line, it will give you the input format. It sounds like you might have a newer version of Matlab that has updated the function and no longer requires as many inputs. Look at the input format form the help file and compare it to the book's expectations.

While you're at it, you might want to just get comfortable with the whole help file and the help functions in general. If the help function isn't enough, try the doc function, "doc regress" which will give you the full documentation on the regress function.

3. Apr 8, 2012

### roam

Thank you very much for the response, I am using the 2012 version of Matlab. When I used 'help regress' and 'doc regress' the help file says these are the acceptable syntaxes:

Code (Text):
b = regress(y,X)
[b,bint] = regress(y,X)
[b,bint,r] = regress(y,X)
[b,bint,r,rint] = regress(y,X)
[b,bint,r,rint,stats] = regress(y,X)
[...] = regress(y,X,alpha)
But the question really asks that I incorporate the root-mean-square error and the order into my code. How can I do that using this kind of command?

Also when I used the syntax [p,sp]=regress(d,t) I got another error:

Code (Text):
Error using regress
Y must be a vector and must have the same number of rows as X.
I'm not sure what the problem is since both vecotors contain 13 elements...

4. Apr 8, 2012

### Pythagorean

you can easily write your own program to compute rms

5. Apr 8, 2012

### Pythagorean

transpose both d and t to eliminate that error, btw. That is, do the regress on d' and t'. It's expecting nx1 not 1xn. And you have 13 elements, not 12 :)

6. Apr 10, 2012

### roam

Thanks a lot! :) That solved the error. But now how can I find and plot the root mean square error in each travel time (since I can't have sigmat inside the argument)?

This is the plot so far:

http://img692.imageshack.us/img692/1043/plotqyc.jpg [Broken]

Last edited by a moderator: May 5, 2017
7. Apr 12, 2012

### roam

I'm not quite sure what you meant... is there a special code for the computation of the rms?

8. Apr 12, 2012

### Pythagorean

the rms is the root-mean-square; three relatively simple operations. Maybe I'm not sure what you're question is...

9. Apr 12, 2012

### roam

I believe the question wants us to find the rms error for each travel time, and then include them on the plot as error bars that represent +/- standard errors.

So for the rms I used the code

Code (Text):
sigmat=sqrt(sum(((t-tf).^2)/(N-2)));
I think for previous versions of Matlab you could simply include "sigmat" inside the argument for regress and that would give you the correct plot. But as I mentioned earlier, my version of Matlab only takes in 3 inputs. So how else can I implement this?

10. Apr 12, 2012

### Pythagorean

Oh, so you're problem is really jus a data visualization one? You already have the data?

Check out the errorbar command (go to help or doc)

11. Apr 20, 2012

### roam

I tried that, I'm still getting an error while using the errorbar command. This is the error I am getting:

Code (Text):
Error using errorbar (line 74)
X, Y and error bars must all be the same length

Error in Q3 (line 36)
errorbar(x,y,sy);
This is the code I've used:

Code (Text):
clc;
clf;

N=length(x);

x=1900:-100:700;

y=[2.62 2.37 2.35 2.25 2.09 1.86 1.78 1.64 1.53 1.35 1.18 1.09 0.89];

plot(x,y, 'm*');

p = polyfit(x,y,1);
hold on
plot(d,polyval(p,x))

yf=polyval(p,x);

for i = 1:1:N

sy=sqrt(sum(((y-yf).^2)/(N-2)))

end

ylabel('Travel Time (ms)')
xlabel('Distance (mm)')

A=[sum((x.^2)./(sy.^2)) sum((x)./(sy.^2));sum((x)./(sy.^2)) sum(1./(sy.^2))];
a=[sum((x.*y)./(sy.^2));sum((y)./(sy.^2))];
p=A\a;
delta=(sum(1./(sy.^2)).*(sum((x.^2)./(sy.^2))))-((sum((x)./(sy.^2))).^2);
sa=sqrt((1./delta).*(sum((x.^2)./(sy.^2))));
sb=sqrt((1./delta).*(sum(1./(sy.^2))));
sp=[sb;sa]
errorbar(x,y,sy);
I really don't understand why I am getting this error because when I run my code I get 13 values of sy (the error), x and y also have a length of 13. So what could be the problem?

In my code I also wanted to calculate the coefficient p(1) of the straight line fit tf=p(1).*d+p(2) but once the code is run I get:

Code (Text):
sp =

0 + 0.0000i
0 + 0.0138i
Which doesn't seem like a correct value for p. Not sure what's wrong here...

12. Apr 20, 2012

### Pythagorean

Use the size command on each vector. Sometimes a Nx1 and a 1xN are not seen as the same length; then you just transpose the miscreant.