Troubleshooting Matlab Code: Tips and Tricks for Beginners

  • Thread starter Thread starter eliassiguenza
  • Start date Start date
  • Tags Tags
    Matlab
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:
Hello everyone, I’m considering a point charge q that oscillates harmonically about the origin along the z-axis, e.g. $$z_{q}(t)= A\sin(wt)$$ In a strongly simplified / quasi-instantaneous approximation I ignore retardation and take the electric field at the position ##r=(x,y,z)## simply to be the “Coulomb field at the charge’s instantaneous position”: $$E(r,t)=\frac{q}{4\pi\varepsilon_{0}}\frac{r-r_{q}(t)}{||r-r_{q}(t)||^{3}}$$ with $$r_{q}(t)=(0,0,z_{q}(t))$$ (I’m aware this isn’t...
Hi, I had an exam and I completely messed up a problem. Especially one part which was necessary for the rest of the problem. Basically, I have a wormhole metric: $$(ds)^2 = -(dt)^2 + (dr)^2 + (r^2 + b^2)( (d\theta)^2 + sin^2 \theta (d\phi)^2 )$$ Where ##b=1## with an orbit only in the equatorial plane. We also know from the question that the orbit must satisfy this relationship: $$\varepsilon = \frac{1}{2} (\frac{dr}{d\tau})^2 + V_{eff}(r)$$ Ultimately, I was tasked to find the initial...

Similar threads

Back
Top