MATLAB Answers

0

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

Asked by Shanjul Shrivastava on 20 Oct 2019 at 8:05
Latest activity Answered by Steven Lord
on 20 Oct 2019 at 12:44
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 Comments

Sign in to comment.

2 Answers

Answer by KALYAN ACHARJYA on 20 Oct 2019 at 8:36
Edited by KALYAN ACHARJYA on 20 Oct 2019 at 8:37

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 Comments

Sign in to comment.


Answer by Steven Lord
on 20 Oct 2019 at 12:44

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 Comments

Sign in to comment.