# Plotting a function while using two formulas

I want to plot the following function (fin) is the variable (that has to be on the x-axis):

y[fin]:=2*fin*(2*T*Ud - ((Ud)/(fin)) + ((Uin*Sin[2*Pi*fin*T])/(2*Pi*fin)) +
c*Rb*(2*Ud - Uin)*(Exp[-((T)/(c*Rb))] - 1))


Now Ud,Uin,Rb and care constants, for example

Ud = 0.7, Uin = 5, Rb = 10 , c = 150*10^(-6)


But to find T I need to solve:

FindRoot[
Abs[Uin*Sin[2*Pi*fin*T - (Pi/2)]] - 2*Ud == (Uin - 2*Ud)*Exp[-T/(c*Rb)]
, {T, 1/(4*fin), 1/(2*fin)}
, WorkingPrecision -> 20
]


Now how can I plot the function y[fin] while varying fin?

• Can you include example values of the constants, so the code can just be evaluated directly. – KraZug Jun 19 '18 at 14:11
• @KraZug The constants are fixed (T also) but T is a value that depends on fin and that is the variable that needs to be plotted on the x-axis – asd Jun 19 '18 at 14:15
• fixed to be what? 1? 1000000? – KraZug Jun 19 '18 at 14:16
• @KraZug For example Ud=0.7, Uin=5, Rb=10 and c=150*10^(-6) – asd Jun 19 '18 at 14:17

Not particularly elegant, but does the job here while not modifying your code too much:

Ud = 0.7; Uin = 5; Rb = 10; c = 150*10^(-6);
y[fin_] = 2*fin*(2*T*Ud - ((Ud)/(fin)) + ((Uin*Sin[2*Pi*fin*T])/(2*Pi*fin)) +
c*Rb*(2*Ud - Uin)*(Exp[-((T)/(c*Rb))] - 1));
Teqn[fin_] = Abs[Uin*Sin[2*Pi*fin*T - (Pi/2)]] - 2*Ud == (Uin - 2*Ud)*Exp[-T/(c*Rb)];
Tsol[fin_?NumericQ] := FindRoot[Teqn[fin], {T, 1/(4*fin), 1/(2*fin)}]


Note that you need an underscore at the end of the pattern for y[fin_] - else you'll only store a definition for exactly y[fin] and not y for example.

ListPlot[Table[{fin, y[fin] /. Tsol[fin]}, {fin, 0.01, 1000, 0.01}]] You can find much more solutions, infinitly many.

{Ud = 7/10, Uin = 5, Rb = 10 , c = 150*10^(-6)}

y[fin_] =
2*fin*(2*T*Ud - ((Ud)/(fin)) + ((Uin*Sin[2*Pi*fin*T])/(2*Pi*fin)) +
c*Rb*(2*Ud - Uin)*(Exp[-((T)/(c*Rb))] - 1)) /. T -> T[fin] //
Simplify

eq1 = Abs[Uin*Sin[2*Pi*fin*T - (Pi/2)]] - 2*Ud == (Uin - 2*Ud)*Exp[-T/(c*Rb)]


Plot all graphs satisfying eq1

cp = ContourPlot[Evaluate[eq1], {fin, 0, 3}, {T, 0, 3},
PlotPoints -> 50, ContourStyle -> Red] Find the functions T[fin] with a differential equation. Therefore find starting values as fin==2

nsol = NSolve[(eq1 /. fin -> 2 /. T -> T2) && 0 < T2 < 3, T2]

(*   {{T2 -> 0.102416}, {T2 -> 0.397584}, {T2 -> 0.602416}, {T2 ->
0.897584}, {T2 -> 1.10242}, {T2 -> 1.39758}, {T2 ->
1.60242}, {T2 -> 1.89758}, {T2 -> 2.10242}, {T2 -> 2.39758}, {T2 ->
2.60242}, {T2 -> 2.89758}, {T2 -> 0.147584}, {T2 ->
0.352416}, {T2 -> 0.647584}, {T2 -> 0.852416}, {T2 ->
1.14758}, {T2 -> 1.35242}, {T2 -> 1.64758}, {T2 -> 1.85242}, {T2 ->
2.14758}, {T2 -> 2.35242}, {T2 -> 2.64758}, {T2 -> 2.85242}}   *)

pts = {2, T2} /. nsol;

deq1 = D[eq1 /. T -> T[fin] /. Abs[rr_] -> Sqrt[rr^2], fin] // Simplify

ndsol = NDSolve[{deq1, T == (T2 /. nsol)}, T, {fin, 0, 1000}];


(Ignore error message for singular overflow.)

Plot T[fin] in an area larger than 1/(4*fin) < T < 1/(2*fin) .

pl = Plot[T[fin] /. First@ndsol, {fin, 0, 10}, Epilog -> Point[pts],
PlotRange -> {0, 3}, AspectRatio -> 1];

rp = RegionPlot[1/(4*fin) < T < 1/(2*fin), {fin, 0, 10}, {T, 0, 3},
PlotPoints -> 100];

Show[pl, cp, rp, Epilog -> Point[pts]] Now get a few of the infinite many y[fin]

Plot[y[fin] /. First@ndsol, {fin, 0, 1000}] It also yields the solution, @KraZug found.

Plot[y[fin] /. First@ndsol, {fin, 0, 1000}, PlotRange -> {0, 3.3}] 