Function with double arguments
    11 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Claudia Santos
 le 31 Jan 2016
  
    
    
    
    
    Commenté : Walter Roberson
      
      
 le 31 Jan 2016
            Can I make this?
function[g]= forward(f(x),x)
    for j=5:5:40;
       h=inv(j)
       g=(f(x)+4*f(h)-f(h+h))/(h+h)  
    end
    end
1 commentaire
Réponse acceptée
  the cyclist
      
      
 le 31 Jan 2016
        
      Modifié(e) : the cyclist
      
      
 le 31 Jan 2016
  
      You want to call a MATLAB function an argument that is itself a function, right? You can do that as follows. Define your function like this:
function [g] = forward(f,x)
      for j=5:5:40;
         h = inv(j);
         g = (f(x)+4*f(h)-f(h+h))/(h+h);
      end
end
and call it using a "function handle" like this:
forward(@sin,7) % Argument is the MATLAB built-in sine function
or like this
my_func = @(x) x^2; % Define your own function, and assign its handle
forward(my_func,7) % Argument is function defined by you
2 commentaires
  the cyclist
      
      
 le 31 Jan 2016
				So, if you now do
my_func(5)
you will get 25 as output.
The best form of thanks is to upvote and/or accept solutions that helped you. This rewards the contributor, and can point future users to helpful answers.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


