MATLAB linear fit problem

1 visualizzazione (ultimi 30 giorni)
Erin Szaruga
Erin Szaruga il 27 Nov 2018
Commentato: Erin Szaruga il 27 Nov 2018
2) Modify your linear least squared code so that it can fit data to a power function and plot both the data as scatter and the best fit curve as a smooth function on the same set of axes. Submit the modified code and, once it is working, a graph of period versus orbital radius for the Jovian moons. Write out/print the best fit power function equation that your code generated.
This is the question and I have my data in a file called ' moon_data', I try and run the data through a script file I have for finding the linear fit called 'get_linear_fit_2".
I keep getting a "too many output arguments" error when it runs the data thru my cramers rule script that I have already used many times in the past. I can't seem to figure out how this is, can somebody help me?
function fit= get_linear_fit_2(data)
s = size(data)
Syx=0
Sxx=0
Sy=0
Sx=0
for n=1:s(1)
Syx=data(n,1)*data(n,2)+Syx
Sxx=(data(n,1))^2+sxx
Sx=data(n,1)+Sx
Sy=data(n,2)+Sy
end
m=[Sxx,Sx;Sx,s(1)]
c=[Syx;Sy]
fit=Cramers_rule(m,c)
end
%Linear fit data above
function Cramers_rule(m,c)
s=size(m)
n=1
dm=det(m)
while s(2)>=n
a=m
a(:,n)=c(:)
y(n)=det(a)/dm
disp(y(n))
n=n+1
end
end
%Cramers Rule function above
My data code is just the two data sets in a script file alone, thats it.
Please help!!

Risposte (1)

Image Analyst
Image Analyst il 27 Nov 2018
You say
fit=Cramers_rule(m,c)
as if Cramers_rule() returns something. You can see from your function declaration:
function Cramers_rule(m,c)
that is does NOT return anything, so there is nothing that can be assigned to your "fit" variable. Hence the error message you got.
  1 Commento
Erin Szaruga
Erin Szaruga il 27 Nov 2018
Okay that makes sense to me but what do I change about my code that would make it so MATLAB runs this thru and returns a fit for a power function?

Accedi per commentare.

Categorie

Scopri di più su Interpolation in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by