Troubleshooting Matlab Code: Tips and Tricks for Beginners

  • Thread starter Thread starter eliassiguenza
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
The user is struggling with MATLAB code, specifically receiving an error message about too many input arguments in the `regress` function. The provided script attempts to perform a regression analysis using vectors for x, y, and sy, but the function call is incorrect. A suggestion is made to refer to the MATLAB documentation for `regress`, which indicates that the first two arguments should be in matrix form. The discussion emphasizes the importance of correctly formatting inputs for MATLAB functions to avoid errors.
eliassiguenza
Messages
24
Reaction score
0
Anyone Help Matlab! Please!?

I am pretty bad at using matlab, i have never used it before and i don't know what's wrong with my code, i would honestly appreciate if someone could give me a hand.

Script:
x=linspace(0,5,10)';
y=[2.75 4.83 5.05 6.80 8.83 8.64 11.03 13.20 13.08 15.68]';
sy=[0.5 0.6 0.7 0.8 0.9 0.9 1.0 1.1 1.1 1.2]';
[c,sc]=regress(x,y,sy,1);
plot(x,polyval(c,x),x,y,'o');
errbar(x,y,sy)

Function:

function sc = cerror_2(x,y,sy)
% This function calculates the errors in the fitted coefficients a and b for
% a straight line fitting problem.

A=(sum (x.^2) ./ (sy.^2));

B=(sum (1 ./ sy.^2));

C=(((sum (1./sy.^2) * ((sum (x.^2) ./ (sy.^2))) - (sum (x./sy.^2)))));

sa = (1./C.*A);

sb = (1./C.*B);

sy = [sb,sa];


end

Error

? Error using ==> regress
Too many input arguments.

Error in ==> Problem11_Executer at 4
[c,sc]=regress(x,y,sy,1);
 
Physics news on Phys.org


eliassiguenza said:
I am pretty bad at using matlab, i have never used it before and i don't know what's wrong with my code, i would honestly appreciate if someone could give me a hand.

Script:
x=linspace(0,5,10)';
y=[2.75 4.83 5.05 6.80 8.83 8.64 11.03 13.20 13.08 15.68]';
sy=[0.5 0.6 0.7 0.8 0.9 0.9 1.0 1.1 1.1 1.2]';
[c,sc]=regress(x,y,sy,1);
plot(x,polyval(c,x),x,y,'o');
errbar(x,y,sy)

Function:

function sc = cerror_2(x,y,sy)
% This function calculates the errors in the fitted coefficients a and b for
% a straight line fitting problem.

A=(sum (x.^2) ./ (sy.^2));

B=(sum (1 ./ sy.^2));

C=(((sum (1./sy.^2) * ((sum (x.^2) ./ (sy.^2))) - (sum (x./sy.^2)))));

sa = (1./C.*A);

sb = (1./C.*B);

sy = [sb,sa];end

Error

? Error using ==> regress
Too many input arguments.

Error in ==> Problem11_Executer at 4
[c,sc]=regress(x,y,sy,1);

It seems to me MATLAB doesn't like the way you call the function regress. See http://www.mathworks.com/help/toolbox/stats/regress.html". If you need any help just ask but i think you should get the answer by reading the link i gave you! You have to many arguments and i think you want to put the first 2 arguments in a matrix form.

P.S. i don't think this is advanced physics!
 
Last edited by a moderator:

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K