Undefined operator '>' for input arguments of type 'function_handle'. Error in simple_nlr (line 24) while err > tol
3 次查看(过去 30 天)
显示 更早的评论
clear all; close all; clc;
%Non Linear Regression
%Model: y = a1*a2^x
%measured data
xm = [50 450 780 1200 4400 4800 5300]';
ym = [28 30 32 36 51 58 69]';
if length(xm) ~= length(ym)
disp('error : xm and ym datasets have different sizes')
end
%define tolerance
tol = 1e-5;
%make intelligent initial guess
a0 = [1 1]
%compute function y
y = @(a) a(1)*a(2).^xm
err = @(a) sum((y(a) - ym).^2)
while err > tol
yopt = fmincon(err,a0);
end
%plots
plot (xm,ym,'ro')
hold on
plot (xm,y,'bx')
Please tell me how to rectify this error and to avoid future mistakes
0 个评论
回答(2 个)
KALYAN ACHARJYA
2019-10-20
编辑:KALYAN ACHARJYA
2019-10-20
The err in your code is function handle
>> err
err =
function_handle with value:
@(a)sum((y(a)-ym).^2)
You are trying to compare it with numeric value (tol), read about compare function handle
0 个评论
Steven Lord
2019-10-20
You don't want to ask if the function handle is greater than a certain value.
You want to ask if the result of evaluating the function handle at a certain point is greater than a certain value.
fh = @sin;
thisWillError = fh > 0.5
thisWillWork = fh(pi/2) > 0.5
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!