embedded function with symbolic

3 visualizaciones (últimos 30 días)
pradeep kunwar
pradeep kunwar el 30 de Dic. de 2020
Comentada: Walter Roberson el 10 de Mzo. de 2021
I have a equtions likes
y=f(7,2,0)+f(-1,2,0)+f(0,0,-1)+f(1,1,0)+f(0,-1,0)
if any inside the function is -ve , the function is zero
for example
f(-1,2,0)= 0 because there is a negative terms.
final answer is y =f(7,2,0)+f(1,1,0) because other function f have -ve terms.
Could you help me.

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Dic. de 2020
Editada: Walter Roberson el 30 de Dic. de 2020
You can use mapSymType to process this
mapSymType(y, 'f', @(X)piecewise(any(cellfun(@(N) isAlways(N<0, 'unknown', false),children(X)) ), 0, X)
...something like that
mapSymType can be a bit of a nuisance. The object passed to the anonymous function would be the function call like f(-1,2,0) and you have to use children() to get at the arguments, which gives you a cell array so you have to cellfun...
  2 comentarios
pradeep kunwar
pradeep kunwar el 4 de En. de 2021
Could you, please explan about ''@(X)piecewise(any(cellfun(@(N) isAlways(N<0, 'unknown', false)"
What are X and cellfun(@(N)) ?
Walter Roberson
Walter Roberson el 10 de Mzo. de 2021
@(X) followed by an expression in X indicates an anonymous function. MATLAB takes the part after the @(X) and builds code for it, replacing the name X with a reference to the first parameter passed to the function.
Likewise the @(N) introduces a second anonymous function used within the first one.
The effect is like
mapSymType(y, 'f', @anonymous0)
function result0 = anonymous0(X)
result0 = piecewise(expression1(X), 0, X);
end
function result1 = expression1(X)
result1 = any( cellfun(@anonymous2, children(X)) )
end
function result2 = anonymous2(N)
result2 = isAlways(N<0, 'unknown', false)
end
So, look for symbolic function calls to f() . If you find one, replace it with a piecewise() construct that returns 0 if a condition is true and returns the original expression otherwise. The condition to be tested is to take all of the arguments to the function (children of a symbolic function call is the list of parameters passed to the function), and for each one, if you can prove that it is always negative, then the condition is true.
So take a parameter to a call to f, and if you can prove the parameter is always negative, return true, and if true was returned for any parameter, replace the entire call with 0.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by