symbolic math - The summation of piecewise functions in Matlab -
i have piecewise function f_m(x)
below, x_m
, \alpha
constants, m
subscript.
i need solve following equation involves f_m(x)
, n
, c
constants, tau
variable want solve.
so resort question matlab. first write function in .m file:
function y=pareto_cdf(x, xm) alpha = 2; %set alpha 2 if x < xm y = 0; else y = 1-(xm/x)^alpha; end
then in .m file write:
syms x; c = 1000; n = 20000; %pareto distribution cdf: f(x)=1-(xm/x)^alpha. %alpha set 2 in program alpha = 2; j=1:1:n; f=sum(pareto_cdf(x, 1/(alpha*rand()))); = f - c; tau = solve(a,'x');
however, got following error matlab when running it:
conversion logical sym not possible. error in pareto_cdf (line 4) if x < xm
the problem is: need declare x
sym
because variable in equation, since invoke pareto_cdf(x, xm)
, x
in function sym
, matlabt not support comparison between sym
, value (xm). error stems piecewise nature of function f(x)
.
so, how can solve problem?
using heaviside()
function resolves problem. rewrite function .m file follows:
function y=pareto_cdf(x, xm) alpha = 2; %set alpha 2 y = heaviside(x-xm).*(1-(xm/x).^alpha);
the reason use element-by-element product/division (.*, ./) because input may vector.
Comments
Post a Comment