how to rewrite f = @(x) x^2 - 5 without using the @ symbol in matlab

You can define a function handle without using the @ symbol in MATLAB as follows:

main.m
function fval = myfun(x)
    fval = x^2 - 5;
end
49 chars
4 lines

Here, we define a function myfun that takes a variable x and returns its squared value, minus 5.

You can then use this function handle in the same way as before:

main.m
>> fval = myfun(3)
fval =
     4
33 chars
4 lines

Note that you need to save this function definition in a separate file with the same name as the function (myfun.m in this case) in order to use it in the MATLAB environment.

related categories

gistlibby LogSnag