1
$\begingroup$

I have a function,I want to plot it

enter image description here

This piecewise function looks very complicated, because the boundary changes as c changes. want to just Plot it, not Manipulate

To solve this problem, I first do it manually, one by one.Here c takes on three special values,like this

enter image description here

Then use the Show function to integrate them enter image description here

This is very complicated and requires constant manual operation,Therefore, I improved my approach by discretizing C, defining functions of each interval separately, and finally integrating them with piecewise functions

(*=== 常数 ===*)
Clear["Global`*"]
rf = 0;
theta = 0.1;
p = 1;

(*=== 边界 ===*)
boundary1 = 0;
boundary2 = c;
boundary3 = 
  c*(2 - c (1 + rf)/(1 - theta)*p + (1 - theta)*p/(1 + rf) - c) + 
   1/2 ((1 - theta)*p/(1 + rf) - c)^2;
boundary4 = 2;

(*=== 分段函数定义 ===*)
funb1[b_] := 0;
funb2[b_] := -c - (2^(1/3)*(-6 b + 12 c - 
        3 c^2))/(3 *(54 c^2 + 
         Sqrt[2916 c^4 + 4 (-6 b + 12 c - 3 c^2)^3])^(1/
         3)) + (54 c^2 + 
       Sqrt[2916 c^4 + 4*(-6 b + 12 c - 3 c^2)^3])^(1/3)/(3*2^(1/3));
funb3[b_] := (1 - theta)*p/(1 + rf) - c;

fun = Table[(Through[{funb1, funb2, funb3}@{x}] // Flatten), {c, 0.3, 
    0.8, 0.01}];
boundary = 
  Table[{boundary1, boundary2, boundary3, boundary4}, {c, 0.3, 0.8, 
    0.01}];

I'm discretizing C, so I'm using these two lines of code

fun = Table[(Through[{funb1, funb2, funb3}@{x}] // Flatten), {c, 0.3, 
    0.8, 0.01}];
boundary = 
  Table[{boundary1, boundary2, boundary3, boundary4}, {c, 0.3, 0.8, 
    0.01}];

After I discretize C, then I want to define the piecewise function that I want once and for all, but I don't know how to do it, and defining the piecewise function manually would be very, very tedious.

Please help me. Thank you

$\endgroup$
4
  • $\begingroup$ Are you aware of the command Piecewise see here? $\endgroup$
    – user293787
    Jun 18 at 7:50
  • $\begingroup$ @user293787 I know that, but I don't know how to define it all at once, I have to do it manually, okay $\endgroup$ Jun 18 at 7:53
  • $\begingroup$ Your question contains a lot of info, some of which may not be relevant to your actual problem, so let me take something simpler such as f[c_,x_] := Piecewise[{{0,0<=x<=c},{x-c,c<=x<=2*c},{c,2*c<=x<=2}}]. Suppose we then set p[c_] := Plot[f[c,x],{x,0,2}]. Then p[0.2] and p[0.7] will give you two plots. Does this help? $\endgroup$
    – user293787
    Jun 18 at 8:04
  • $\begingroup$ @user293787 yes,this is a good idea,use two vars $\endgroup$ Jun 18 at 8:11

1 Answer 1

4
$\begingroup$

Set all of the functions depend on c.

(*===常数===*)Clear["Global`*"]
rf = 0;
theta = 0.1;
p = 1;

(*===边界===*)
boundary1[c_] = 0;
boundary2[c_] = c;
boundary3[c_] = 
  c*(2 - c (1 + rf)/(1 - theta)*p + (1 - theta)*p/(1 + rf) - c) + 
   1/2 ((1 - theta)*p/(1 + rf) - c)^2;
boundary4[c_] = 2;

(*===分段函数定义===*)
funb1[c_][b_] = 0;
funb2[c_][
   b_] = -c - (2^(1/3)*(-6 b + 12 c - 
        3 c^2))/(3*(54 c^2 + 
         Sqrt[2916 c^4 + 4 (-6 b + 12 c - 3 c^2)^3])^(1/
         3)) + (54 c^2 + 
       Sqrt[2916 c^4 + 4*(-6 b + 12 c - 3 c^2)^3])^(1/3)/(3*2^(1/3));
funb3[c_][b_] = (1 - theta)*p/(1 + rf) - c;
f[c_][b_] = 
  Which[boundary1[c] <= b <= boundary2[c], funb1[c][b], 
   boundary2[c] < b < boundary3[c], funb2[c][b], 
   boundary3[c] <= b <= boundary4[c], funb3[c][b]];
Plot[Table[f[c][b], {c, .3, .8, .1}] // Evaluate, {b, 0, 1.5}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.