nlinfit command problem :(

9 Ansichten (letzte 30 Tage)
Enes Emre Öztürk
Enes Emre Öztürk am 16 Jan. 2021
Bearbeitet: Stephen23 am 16 Jan. 2021
A gaussfit code gives an error of:
Error using nlinfit>checkWeights (line 961)
Weights must be a vector of real positive weights or a function handle that accepts a vector of
predicted response values and returns a vector of real positive weights the same size as Y.
Error in nlinfit (line 223)
nanweights = checkWeights(weights, yfit, y);
Error in Nonlinearhw (line 8)
plot(q,Astart,'-k'); [beta,R,J,covB]=nlinfit(q,A,'gaussfit',n,'Weights',dA);
Also q and A both are 161*1 matrixes.
What is the problem?? :(
  1 Kommentar
Stephen23
Stephen23 am 16 Jan. 2021
Bearbeitet: Stephen23 am 16 Jan. 2021
Original question by Enes Emre Öztürk:
nlinfit command problem :(
clc; clear all; close all;
[verim]=specokuma('HWdata.spc',403);
q=verim(:,1);A=verim(:,end);
B=q+A;dA=sqrt(verim(:,end));
figure(1);hold on; errorbar(q,A,dA,'og');
n=[1000 8 0.01 1000];
[Astart]= gaussfit(n,q); hold on;
plot(q,Astart,'-k'); [beta,R,J,covB]=nlinfit(q,A,'gaussfit',n,'Weights',dA);
[Afit]=gaussfit(beta,q);
plot(q,Afit,'-r','Linewidht',3);
And gaussfit function is:
function [Afit]=gaussfit(n,X)
Amp=abs(n(1));
Xc=n(2);
width=abs(n(3));
backgnd=(n(4)); %yb=n(4)+n(5)X
%Afit=(n(1)*exp(-1*(X-n(2)).^2/(n(3)^2))+n(4));
Afit=(Amp*exp(-1*(X-Xc).^2/(width^2))+backgnd);
İt gives an error of:
Error using nlinfit>checkWeights (line 961)
Weights must be a vector of real positive weights or a function handle that accepts a vector of
predicted response values and returns a vector of real positive weights the same size as Y.
Error in nlinfit (line 223)
nanweights = checkWeights(weights, yfit, y);
Error in Nonlinearhw (line 8)
plot(q,Astart,'-k'); [beta,R,J,covB]=nlinfit(q,A,'gaussfit',n,'Weights',dA);
Also q and A both are 161*1 matrixes.
What is the problem?? :(

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 16 Jan. 2021
From the error message: "Weights must be a vector of real positive weights ..." Are they?
  • Is dA a vector? Check with isvector.
  • Is dA a real vector? Check with isreal.
  • Are all the elements in dA positive? Check using the > operator. Remember that 0 is not positive.
  1 Kommentar
Steven Lord
Steven Lord am 16 Jan. 2021
Since the poster edited away most of the code (please don't do that in the future, it can make the answer to the question useless for people trying to solve the same problem in the future) one of the lines in the error stack lists the line of code that told me dA was the vector being specified as the weights.
Error in nlinfit (line 223)
nanweights = checkWeights(weights, yfit, y);
Error in Nonlinearhw (line 8)
plot(q,Astart,'-k'); [beta,R,J,covB]=nlinfit(q,A,'gaussfit',n,'Weights',dA);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by