how handle piece-wise functon in matlab, example
function f= analiticalf(t1,t2,t,curve) %solved in maxima %solve([a3*t1^3+a2*t1^2+a1*t1+a0= l1, a3*t2^3+a2*t2^2+a1*t2+a0= l2, 3*a3*t1^2+2*a2*t1+a1= 0, 3*a3*t2^2+2*a2*t2+a1= 0],[a3,a2,a1,a0]); l1= curve(t1); l2= curve(t2); if (t <= t1) f= @(t)l1; elseif(t >= t2) f= @(t)l2; else a3=-(2*l1-2*l2)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); a2=(-3*t2*l2-3*t1*l2+(3*t2+3*t1)*l1)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); a1=-(6*t1*t2*l1-6*t1*t2*l2)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); a0=(-3*t1^2*t2*l2+t1^3*l2+(3*t1*t2^2-t2^3)*l1)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); f= @(t)a3*t^3 + a2*t^2 + a1*t + a0; end end then want apply function array of elements:
f= analiticalf(t1,t2,t,curve); y= arrayfun(f,trange); but problem need specify t in analiticalf(t1,t2,t,curve) , want t parameter free. need 'smart' handle of function f such call f(100) can determine part of piece-wise function use without specifying parameter t.
you can use indicator functions! example if if want function f(t) = t when t < 0 , f(t) = t^2 when t >= 0, can define:
f = @(t) t*(t<0) + t^2*(t>=0); this can of course generalized more advanced functions 1 of yours!
i hope solves problem.
edit: decided throw in solution function:
function f= analiticalf(t1,t2,curve) l1= curve(t1); l2= curve(t2); a3=-(2*l1-2*l2)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); a2=(-3*t2*l2-3*t1*l2+(3*t2+3*t1)*l1)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); a1=-(6*t1*t2*l1-6*t1*t2*l2)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); a0=(-3*t1^2*t2*l2+t1^3*l2+(3*t1*t2^2-t2^3)*l1)/(-t2^3+3*t1*t2^2-3*t1^2*t2+t1^3); f = @(t) (t <= t1)*l1 + (t >= t2)*l2 + ((t > t1)&(t<t2))*(a3*t^3 + a2*t^2 + a1*t + a0); end
Comments
Post a Comment