nlinfit command problem :(

조회 수: 10 (최근 30일)
Enes Emre Öztürk
Enes Emre Öztürk 2021년 1월 16일
편집: Stephen23 2021년 1월 16일
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
Stephen23
Stephen23 2021년 1월 16일
편집: Stephen23 2021년 1월 16일
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?? :(

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2021년 1월 16일
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
Steven Lord
Steven Lord 2021년 1월 16일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by