nlinfit command problem :(

2 次查看(过去 30 天)
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by