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

1 visualizzazione (ultimi 30 giorni)
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

Risposte (2)

KALYAN ACHARJYA
KALYAN ACHARJYA il 20 Ott 2019
Modificato: KALYAN ACHARJYA il 20 Ott 2019
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 il 20 Ott 2019
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

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by