Troubleshooting Matlab Code: Tips and Tricks for Beginners

  • Thread starter Thread starter eliassiguenza
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The forum discussion addresses a common error encountered in MATLAB when using the regress function. The user attempts to call regress with too many input arguments, leading to the error message "Too many input arguments." The correct usage of regress requires the first two arguments to be combined into a matrix format. The discussion also includes a custom function, cerror_2, designed to calculate errors in fitted coefficients for linear regression.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with linear regression concepts
  • Knowledge of error handling in MATLAB
  • Experience with plotting functions in MATLAB
NEXT STEPS
  • Review the MATLAB documentation for the regress function
  • Learn how to format input data for regression analysis in MATLAB
  • Explore error handling techniques in MATLAB scripts
  • Investigate additional MATLAB plotting functions for data visualization
USEFUL FOR

Beginner MATLAB users, data analysts, and anyone interested in performing linear regression analysis and troubleshooting MATLAB code.

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
2K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · 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