Solving Speed of Sound in a Liquid - Matlab Experiment

Click For Summary

Discussion Overview

The discussion revolves around solving for the speed of sound in a liquid using Matlab, focusing on an experimental setup that involves plotting travel times at various depths. Participants are troubleshooting code errors and exploring how to incorporate root-mean-square (RMS) error into their analysis.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant shares their Matlab code and describes the experiment, seeking help for an error encountered with the 'regress' function.
  • Another participant suggests checking the input format for the 'regress' function, noting potential changes in newer Matlab versions.
  • A participant expresses confusion about incorporating RMS error into the regression command and mentions receiving an error regarding vector dimensions.
  • Several participants discuss the need to transpose vectors to match expected dimensions for the 'regress' function.
  • There is a suggestion to write a custom program to compute RMS error, with some participants clarifying what RMS entails.
  • A participant attempts to implement error bars in their plot using the 'errorbar' command but encounters an error related to vector lengths.
  • Another participant advises using the 'size' command to diagnose potential issues with vector dimensions.

Areas of Agreement / Disagreement

Participants generally agree on the need to address vector dimension issues for the 'regress' function and the computation of RMS error. However, there is no consensus on the best approach to visualize the RMS error or on the specific implementation details in Matlab.

Contextual Notes

Participants mention various versions of Matlab, which may affect function syntax and behavior. There are unresolved issues regarding the correct implementation of error bars and the calculation of coefficients from the regression analysis.

Who May Find This Useful

This discussion may be useful for individuals working on experimental data analysis in Matlab, particularly those interested in regression analysis and error computation in scientific contexts.

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.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
7
Views
3K