# [✓] Solve a piecewise function with NDSolve?

Posted 11 months ago
1155 Views
|
3 Replies
|
3 Total Likes
|
 I have the following code on mathematica paramFinal = {\[Rho] -> 0.05, price -> 0.05, \[Gamma] -> 0.5, \[Omega] -> 0.8, d -> 1, a -> 0.3, b -> 0.1, r -> 0.7, \[Gamma] -> 0.6, Subscript[g, y] -> 0.9, \[Delta] -> 0.1, \[Eta] -> 2.65, \[Psi] -> 1, pbar -> 0.5, hbar -> 0.25, Subscript[\[CapitalOmega], 1] -> 0.5, Subscript[\[CapitalOmega], 2] -> 0.6}; with functional forms f[x_] := x^2 z[x_] := x^2 NDSolve[{ v'[t] == (r v[t] - (price b v[t])/ Piecewise[{{z'[p[t]], pbar > p[t]}, {pbar, pbar < p[t]}}]) /. paramFinal, p'[t] == Piecewise[{{-((p[t] z'[p[t]])/z''[p[t]]) - a v[t] /. paramFinal, pbar > p[t]}, {0, pbar < p[t]}}], v[0] == 0.024, p[0] == 0.025}, v[t], p[t], {t, 0, 100}] This code gives me the following error NDSolve::conarg: The arguments should be ordered consistently. >> I don't see the reason behind since I don't have variables that depend on more than one variable. It depens just on time $t$ or I don't have neither partial derivatives. Thanks a lot in advance !
3 Replies
Sort By:
Posted 11 months ago
 You haven't formatted your code correctly, so it cannot be copied, pasted and tested.However, try {v[t], p[t]}, i.e., putting the expressions to solve for in a list, in NDSolve instead of NDSolve[..., v[t], p[t], {t, 0, 100}].
 Just insert brackets around the unknown functions, and a replacement of parameters: NDSolve[{v'[ t] == (r v[t] - (price b v[t])/ Piecewise[{{z'[p[t]], pbar > p[t]}, {pbar, pbar < p[t]}}]) /. paramFinal, p'[t] == Piecewise[{{-((p[t] z'[p[t]])/z''[p[t]]) - a v[t] /. paramFinal, pbar > p[t]}, {0, pbar < p[t]}}] /. paramFinal, v[0] == 0.024, p[0] == 0.025}, {v[t], p[t]}, {t, 0, 100}]