Function, input number, return a value

QUESTION:
Write a small function which takes as inputs; a number a and a function g(x), and returns (a, g(a)). Note that printing is not the same as returning a value. Use the function g(x) = 2x 2 and a = 4 to test your function.
So my function right now looks like this.
function [a g(x)] = mysmallfunction(x)
a = x;
g(x) = 2*x.^2;
end
And I'm calling it like this:
clear;
[a g(x)] = mysmallfunction(4)
I get the error "Undefined function or variable 'mysmallfunction'."
But also I'm using a specific equation in 2x^2. How do I make it work for any equation.

 Respuesta aceptada

Guillaume
Guillaume el 29 de Abr. de 2015
Editada: Guillaume el 29 de Abr. de 2015
Your tutor must have taught you about function handles for you to solve this problem.
As per the question, the function you have to write takes two inputs, a number and a function (see function handles) and returns two outputs, the original number and another number. Therefore you must have two variable names before the = in your function declaration and two variable names in the brackets of your function declaration.
Note that g(x) is not a valid variable name.
To get you started, the following declaration would work:
function [a, ga] = mysmallfunction(x, g)

5 comentarios

Eric's comment moved here:
Guillaume, 'm kind of familiar with function handles. I didn't really change much.
function [a, ga] = mysmallfunction(x, g)
a = x
ga = @g
end
Called:
clear;
[a, ga] = mysmallfunction(4, 2*x.^2)
type mysmallfunction.m
I get the error 'undefined function or variable of x'
You would use the '@' syntax to create the function (i.e. when you call the function). Within the function itself, where you're using the handle, it's a normal function call.
For example, to create the function g(x) = 2*x^2
g = @(x) 2*x^2;
To use the function
a = 4;
ga = g(a);
That really should be enough for you to finalise your code. As it's homework, I don't want to give the full code.
Eric
Eric el 29 de Abr. de 2015
I'm a little confused.
Are you saying you have to include the function(2x^2) inside the function itself and not when I call the function?
Aren't I suppose to insert the equation when I call the function? Otherwise wouldn't I have to keep on changing the equation(everytime I want to use a different one) in mysmallfunction.m file?
No, you do not include the declaration of the function handle inside your own function, the function handle is one of the argument you pass to your function. That is, to test your function you would call it with
[a, ga] = mysmallfunction(4, @(x) 2*x^2);
Or to be more explicit:
g = @(x) 2*x^2
[a, ga] = mysmallfunction(4, g);
You only need to work out how to use the function handle inside your own function. I've shown you how in my previous comment.
Eric
Eric el 29 de Abr. de 2015
Aha! Thankyou I finally pieced all your hints together! Appreciate it

Iniciar sesión para comentar.

Más respuestas (1)

Pratik Bajaria
Pratik Bajaria el 29 de Abr. de 2015
Hello,
You just have to change a few things and it must work. Atleast it does for me. ;-) you need not write g(x) literally.
function [a g] = mysmallfunction(x)
a = x;
g = 2*x.^2;
end
Call it like this:
clear;
[a g] = mysmallfunction(4)
Check and let me know if it works for you. I assume, i have got your problem right.
Regards, Pratik

2 comentarios

Guillaume
Guillaume el 29 de Abr. de 2015
I don't think you've got it right. The question clearly states that the function has two inputs.
Guillaume
Guillaume el 29 de Abr. de 2015
Eric's comment moved here:
Yes there is two inputs.
Pratik your solution would work for a defined equation.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Abr. de 2015

Comentada:

el 29 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by