Solving Speed of Sound in a Liquid - Matlab Experiment

In summary: It sounds like you are trying to use the regress function to fit a straight line to data. The regress function only works with data that is a vector of the correct size. Your data is not a vector, so the regression function will not work.
  • #1
roam
1,271
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
  • #2
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
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...
 
  • #4
you can easily write your own program to compute rms
 
  • #5
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
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:
  • #7
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?
 
  • #8
the rms is the root-mean-square; three relatively simple operations. Maybe I'm not sure what you're question is...
 
  • #9
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.
 

1. What is the purpose of conducting a speed of sound experiment in a liquid using Matlab?

The purpose of this experiment is to measure the speed of sound in a liquid and understand how it is affected by various factors, such as temperature, pressure, and composition of the liquid. This can provide valuable insights into the properties of the liquid and can also be used for practical applications, such as in the design of acoustic devices.

2. How is the speed of sound calculated in a liquid using Matlab?

The speed of sound in a liquid can be calculated by measuring the time it takes for a sound wave to travel through a known distance in the liquid. This can be done using a speaker to generate a sound wave and a microphone to record the time it takes for the sound to travel to a receiver at a known distance. Matlab can then be used to analyze the data and calculate the speed of sound using the formula: speed = distance/time.

3. What factors can affect the speed of sound in a liquid?

The speed of sound in a liquid can be affected by various factors, such as temperature, pressure, and the composition of the liquid. In general, the speed of sound increases with an increase in temperature and decreases with an increase in pressure. The composition of the liquid, including its density and molecular structure, can also impact the speed of sound.

4. How can the speed of sound in a liquid be used in practical applications?

The speed of sound in a liquid has many practical applications, including in the design of acoustic devices, such as sonar systems, underwater communication systems, and medical ultrasound equipment. It can also be used in industries such as oil and gas, where the speed of sound can be used to detect leaks or measure the density of liquids.

5. Are there any limitations to using Matlab for a speed of sound experiment in a liquid?

While Matlab is a powerful tool for analyzing data and calculating the speed of sound in a liquid, there are some limitations to consider. For example, the accuracy of the results may be affected by the quality of the equipment used, the positioning of the speaker and microphone, and the environmental conditions. Additionally, the assumptions made in the calculation, such as the liquid being incompressible, may also impact the accuracy of the results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
4K
  • Introductory Physics Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
960
  • Introductory Physics Homework Help
Replies
12
Views
3K
  • Introductory Physics Homework Help
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top