Find the input values of a function within an output range.
    13 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    Edward Simpson Songel
 il 9 Giu 2021
  
    
    
    
    
    Commentato: Nigel Brauser
 il 4 Nov 2022
            Let us say we have a funtion like y= @(n) f(n).
How would you find the range of n values for the y range y[ ymin,ymax] is there some sort of inverse function command in MATLAB with which you could do this? I.E. n=@(y) f^(-1)(y) or something similar.
A practical example would be:
Rend = @(n) Pu(n)/Pa(n);   % Where Pu(n) and Pa(n) are other functions which also depend of n.
% Find value range of n for Rend==[R1,R2] where R1 is the min of the range
% and R2 the max of the range.
Thanks in advance.
0 Commenti
Risposta accettata
  Joel Lynch
      
 il 9 Giu 2021
        
      Modificato: Joel Lynch
      
 il 9 Giu 2021
  
      If you cannot solve algebraically (no closed form solution), then fzero is the best bet. Something like this:
% example y range
y = linspace(ymin,ymax); 
% Create empty array of n values of same size
nsol = zeros(size(y));
% Loop y values
for i=1:numel(y)
    % grab local y
    ylocal = y(i); 
    % Setup fzero function function 
    myfunc = @(n) Pu(n)/Pa(n) - ylocal;
    % Solve for myfunc=0
    nsol(i) = fzero( myfunc, nguess );
end
Note that "fzero" is being adapted from solving for 0, to solving for ylocal by subtracting that term. There are probably faster ways to get "myfunc" defined outside the loop. 
1 Commento
  Nigel Brauser
 il 4 Nov 2022
				Thank you very much, this was very helpful. Particularly your simple handling of subtracting ylocal to find the desired value, rather than just 0.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Historical Contests in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


