Undefined operator '>' for input arguments of type 'function_handle'. Error in simple_nlr (line 24) while err > tol

3 ビュー (過去 30 日間)
Shanjul Shrivastava
Shanjul Shrivastava 2019 年 10 月 20 日
回答済み: Steven Lord 2019 年 10 月 20 日
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

回答 (2 件)

KALYAN ACHARJYA
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

Steven Lord
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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by