how to write u= exp(1/((x-x_0)(x-x_1))) for x_0<x<x_1 using '@(x) function' in matlab?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kayode Odeyemi
am 7 Nov. 2019
Kommentiert: Kayode Odeyemi
am 7 Nov. 2019
u =@(x) vif(x>x0&x<x1,exp(1./((x-x0).*(x-x1))),0)
I need a function that can work with the @(x).
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 7 Nov. 2019
vif = @(condition, when_true, when_false) condition .* when_true + (~condition) .* when_false;
Note: this will not work if infinity is produced by the unselected side . For example, vif(x > 0, 1./x, 1) will not work for x = 0: even though the condition x>0 is not true for x=0, the 1./x would still be evaluated, giving inf, and when that was multipled by the 0 (false) of the condition x>0 that would be 0*inf which is nan.
If you need code that is able to handle the case where the expression might give infinity where the condition is not selected, then you will need a true function instead of an anonymous function. (Or you will need some obscure and ugly hacks involving subsexpr() and subsasgn() and multiple helper anonymous functions.)
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!