MATLAB Solving Speed of Sound in a Liquid - Matlab Experiment

AI Thread Summary
The discussion focuses on troubleshooting a Matlab experiment aimed at calculating the speed of sound in a liquid at various depths. The user encounters multiple errors related to the `regress` function and the `errorbar` command, primarily due to input format issues and vector dimensions. Suggestions include checking the input syntax for the `regress` function and ensuring that the vectors used in calculations are properly formatted as column vectors. The user is also guided to calculate and plot root mean square errors as error bars on the graph, emphasizing the importance of matching vector lengths. Overall, the conversation highlights common coding pitfalls in Matlab and strategies for resolving them.
roam
Messages
1,265
Reaction score
12
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


Here is my attempt:

Code:
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:
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:
Physics news on Phys.org
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.
 
Pythagorean said:
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.

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:
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? :confused:

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

Code:
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...
 
you can easily write your own program to compute rms
 
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 :)
 
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)? :confused:

This is the plot so far:

http://img692.imageshack.us/img692/1043/plotqyc.jpg
 
Last edited by a moderator:
Pythagorean said:
you can easily write your own program to compute rms

I'm not quite sure what you meant... is there a special code for the computation of the rms?
 
the rms is the root-mean-square; three relatively simple operations. Maybe I'm not sure what you're question is...
 
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:
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
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
I tried that, I'm still getting an error while using the errorbar command. This is the error I am getting:

Code:
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:
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:
sp =

        0 + 0.0000i
        0 + 0.0138i

Which doesn't seem like a correct value for p. Not sure what's wrong here...
 
  • #12
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.
 
Back
Top