# A problem with inputting a function from the user

4 views (last 30 days)
Jabr on 18 Nov 2022
Commented: Voss on 18 Nov 2022
I used the input function to input a handel-function from the user
The command window keeps asking me to input the fuction again in each time i mentiond the function in the scribt.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%% The General Least Square Regression'
clear all,close all; clc
syms t
disp('The General Least Square Regression ')
ti = input ('Inter the values of t [t0,t1,...] : ');
y = input ('Inter the values of y [y0,y1,...] : ')';
Z0 =@(t) input ('Inter the first bais z0 in terms of t : ');
Z1 =@(t) input ('Inter the second bais z1 in terms of t : ');
Z2 =@(t) input ('Inter the third bais z2 in terms of t : ');
z0=Z0(ti)';
z1=Z1(ti)';
z2=Z2(ti)';
Z=[z0 z1 z2];
Zs=Z'*Z;
ys=Z'*y;
yr=(Zs^-1)*ys;
tplot=[0:0.01:8];
zp0=Z0(tplot);
zp1=Z1(tplot);
zp2=Z2(tplot);
THE COMMAND WINDOW
The General Least Square Regression
Inter the values of t [t0,t1,...] : [1:8]
Inter the values of y [y0,y1,...] : [22 12 9 13 11 11 12 10]
Inter the first bais z0 in terms of t : t.^0
Inter the second bais z1 in terms of t : exp(-t).*cos(10*t)
Inter the third bais z2 in terms of t : (t.^-1).*sin(10*t)
yr =
11.3280
-46.1728
6.6732
Inter the first bais z0 in terms of t : t.^0
Inter the second bais z1 in terms of t : exp(-t).*cos(10*t)
Inter the third bais z2 in terms of t : (t.^-1).*sin(10*t)

Voss on 18 Nov 2022
Edited: Voss on 18 Nov 2022
If you run this line on its own:
Z0 =@(t) input ('Inter the first bais z0 in terms of t : ');
you will notice that no user-input is asked for, which is to say, the input function is not run.
That's because what that line does is: (1) create an anonymous function that runs input, and (2) store the handle to that anonymous function in the variable Z0. But that line does not run the anonymous function, it only defines it.
The anonymous function will be run whenever you call Z0. For instance, running the commmand Z0(ti) calls the anonymous function with argument ti, which prompts the user for input. (The input argument t is not used in the anonymous function and the output returned from input is also unused.)
A similar example of defining and calling an anonymous function:
x = @sin; % defining an anonymous function
x % showing the function definition on the command line (does not call/execute the function)
x = function_handle with value:
@sin
x(pi) % calling the anonymous function with the argument pi executes the function and produces a result
ans = 1.2246e-16
If you want to use the input function to get a function handle from the user, then you can do that like this:
Z0 = input('Enter the first basis[?] z0 in terms of t : ', 's');
Z0 = str2func(['@(t)' Z0]);
Then you can evaluate that user-supplied function at a specific value of t as you have been doing:
Z0(ti)
Z0(tplot)
% etc.
##### 2 CommentsShowHide 1 older comment
Voss on 18 Nov 2022
You're welcome!

### Categories

Find more on NaNs in Help Center and File Exchange

R2014b

### Community Treasure Hunt

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

Start Hunting!