3
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ Can you include example values of the constants, so the code can just be evaluated directly. $\endgroup$
    – SPPearce
    Jun 19, 2018 at 14:11
  • $\begingroup$ @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 $\endgroup$
    – asd
    Jun 19, 2018 at 14:15
  • 1
    $\begingroup$ fixed to be what? 1? 1000000? $\endgroup$
    – SPPearce
    Jun 19, 2018 at 14:16
  • $\begingroup$ @KraZug For example Ud=0.7, Uin=5, Rb=10 and c=150*10^(-6) $\endgroup$
    – asd
    Jun 19, 2018 at 14:17

2 Answers 2

5
$\begingroup$

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[1] for example.

ListPlot[Table[{fin, y[fin] /. Tsol[fin]}, {fin, 0.01, 1000, 0.01}]]

enter image description here

$\endgroup$
3
$\begingroup$

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]

enter image description here

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[2] == (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]]

enter image description here

Now get a few of the infinite many y[fin]

Plot[y[fin] /. First@ndsol, {fin, 0, 1000}]

enter image description here

It also yields the solution, @KraZug found.

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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