![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250444/image.png)
Generate array of y values, from the numerical solution of f(y)=x, where x is an array of numbers
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How can I generate array of y values, from the numerical solution of
, where x is an array of numbers, if I assume that: a. for each x there is only a single y and vice-verse, and b. I cannot invert
and isolate
explicitly?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250448/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250449/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250450/image.png)
For example, let ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250446/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250446/image.png)
This is a monotonically decending function of x, for any
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250447/image.png)
If I have the vector
, how can I obtain the vector of the y values corresponding to these xs ?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250452/image.png)
- I want the array y to contains real numbers, that I can later use for calculations.
- Speed matters, I prefer to find the fastest solution.
Thanks!
0 commentaires
Réponse acceptée
Star Strider
le 26 Nov 2019
There are likely at least two solutions because of the
term. I did not exhaustively analyse the function.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250444/image.png)
Try this:
f = @(y) exp(-y).*(-1+exp(y)-y)./y.^2;
x=[1 2 3 4 5];
for k = 1:numel(x)
ys(k) = fsolve(@(y) f(y)-x(k), 1);
end
Experiment to get different results. The fzero function is also an option, however fsolve is more robust.
3 commentaires
Stephen23
le 26 Nov 2019
"Is there a way to do this without the loop? "
>> x = [1,2,3,4,5];
>> f = @(y) exp(-y).*(-1+exp(y)-y)./y.^2;
>> y = arrayfun(@(v)fzero(@(z)f(z)-v,1),x)
y =
-1 -1.9375 -2.4647 -2.831 -3.1113
But an explicit loop would most likely be faster.
Star Strider
le 26 Nov 2019
As always, my pleasure!
The loop is required, since fsolve (and all the others that I am aware of) can only solve for one value at a time.
For example:
ys = fsolve(@(y) f(y)-x, 1)
only solves for ‘x=3’, and none of the others.
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!