How can I have multiple outputs for a function evaluating various elements?
    3 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Eduardo Chacin
 le 31 Août 2018
  
    
    
    
    
    Modifié(e) : madhan ravi
      
      
 le 1 Sep 2018
            Hi! I'm new into this. I'm writing a function file and I want to be able to input a vector, evaluate each element with the same function and obtain an output that has each element's result. Here is what I'm writing:
function IF=myfunction(P,T)
a=1.28+15*(1/P+0.05)+exp(100/T)
IF=500+a
display (IF)
end
My inputs are P=[10 20] and T=[30 40] and it has an error saying "error using mrdivide; matrix dimensions must agree".
0 commentaires
Réponse acceptée
  madhan ravi
      
      
 le 31 Août 2018
        
      Modifié(e) : madhan ravi
      
      
 le 31 Août 2018
  
      function IF=myfunction(P,T)
a=1.28+15.*(1./P+0.05)+exp(100./T); %USE ELEMENT-WISE OPERATIONS
IF=500+a;
disp(IF) %not Display it’s disp 
end
4 commentaires
  Image Analyst
      
      
 le 31 Août 2018
				The 3 lines in this function can all be combined into a single line like I showed in my solution below.
You use dot slash, dot star, or dot caret when you're want element-by-element multiplication of corresponding array elements at the same index. If you don't you're doing matrix operations, like a matrix multiplication that is the sum of rows from one times columns of the second.
  madhan ravi
      
      
 le 1 Sep 2018
				
      Modifié(e) : madhan ravi
      
      
 le 1 Sep 2018
  
			@Eduardo Chacin like also sir Image Analyst said in addition read the link below for better understanding element wise operations:
Plus de réponses (1)
  Image Analyst
      
      
 le 31 Août 2018
        Use dot slash like this:
function IF = myfunction(P, T)
IF = 501.28 + 15 * (1 ./ P + 0.05) + exp(100 ./ T)
end
0 commentaires
Voir également
Catégories
				En savoir plus sur Matrix Indexing dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


