I have one small question. My code is like follows.
a = 5;
mm = Solve[a x + y == 7 && x - y == 1, {x, y}];
t1 = x /. mm[[1]];
t2 = y /. mm[[1]];
myfunction[{x_, y_}] := (y + x)/(x + r)
Plot[myfunction /@ {{t1, t2}}, {r, 0, 3}, Evaluated -> True]
I want to change plot of myfunction
for different a
in one plot. How could I do? Thanks.
Edit - The actual code is more complex than the stripped down example above and fails to produce the figure showing the function ppw[r] for different values of bb. I have tried the replacement methods suggested by Peltio, but they do not work
Here is a simplified version of the code using only linear ODEs:
xxin[aa_?NumericQ, bb_?NumericQ] :=NDSolve[{\[Psi]''[r] == -bb \[Psi][r], \[Psi]'[0.1] ==0, \[Psi][1] == aa}, \[Psi], {r, 0.1, 1}, PrecisionGoal -> 20]
xxshell[aa_?NumericQ, cc_?NumericQ] :=NDSolve[{\[Psi]''[r] == \[Psi][r] + 1, \[Psi][1] ==aa, \[Psi][1.5] == cc}, \[Psi], {r, 1, 1.5}, PrecisionGoal -> 20]
xxout[cc_?NumericQ] :=NDSolve[{\[Psi]''[r] == \[Psi][r], \[Psi][1.5] ==cc, \[Psi]'[3] == 0}, \[Psi], {r, 1.5, 3}, PrecisionGoal -> 20]
xxinaTry[aa_?NumericQ,bb_?NumericQ] := (Evaluate[\[Psi]'[1] /. xxin[aa, bb]])[[1]]
xxoutaTry[aa_?NumericQ,cc_?NumericQ] := (Evaluate[\[Psi]'[1] /. xxshell[aa, cc]])[[1]]
xxinbTry[aa_?NumericQ,cc_?NumericQ] := (Evaluate[\[Psi]'[1.5] /. xxshell[aa, cc]])[[1]]
xxoutbTry[cc_?NumericQ] := (Evaluate[\[Psi]'[1.5] /. xxout[cc]])[[1]]
bb = 0.8;
sols = FindRoot[{ xxinaTry[aa, bb] == bb xxoutaTry[aa, cc],
xxoutbTry[cc] == xxinbTry[aa, cc]}, {{aa, 0}, {cc, 0}}];
\[Psi]a =aa /. sols;
\[Psi]b = cc /. sols;
pww[r_] = Evaluate[Piecewise[{{Evaluate[ Exp[\[Psi][r]] /. xxin[\[Psi]a, bb]],
0.1 <= r <= 1}, {Evaluate[
Exp[\[Psi][r]] /. xxshell[\[Psi]a, \[Psi]b]],
1 < r < 1.5}, {Evaluate[Exp[\[Psi][r]] /. xxout[\[Psi]b]],
r > 1.5}}, Indeterminate]];
Show[Plot[pww[r], {r, 0, 3}], PlotRange -> All]
Edit 2: By using the above simplified code, I successfully created a plot in the end. (sorry, I do not know how to display a figure here)
But when I modified the pww[r] function into the following:
pww[r_][bb_?NumericQ] = Evaluate[Piecewise[{{Evaluate[ Exp[\[Psi][r]] /. xxin[\[Psi]a, bb]],
0.1 <= r <= 1}, {Evaluate[
Exp[\[Psi][r]] /. xxshell[\[Psi]a, \[Psi]b]],
1 < r < 1.5}, {Evaluate[Exp[\[Psi][r]] /. xxout[\[Psi]b]],
r > 1.5}}, Indeterminate]];
Show[Table[Plot[pww[r][bb], {r, 0, 3}], {bb, 0.8, 0.8, 0.1}], PlotRange -> All]
Here, bb is a variable and I delete the bb=0.8; Then
sols = FindRoot[{ xxinaTry[aa, bb] == bb xxoutaTry[aa, cc], xxoutbTry[cc] == xxinbTry[aa, cc]}, {{aa, 0}, {cc, 0}}];
does not work because it does not know bb. In this case, I tried to replace the \[Psi]a
and \[Psi]b
in the function pww[r_][bb_?NumericQ]
by using the following code:
Evaluate[aa /. FindRoot[{xxinaTry[aa, bb] == bb xxoutaTry[aa, cc],
xxoutbTry[cc] == xxinbTry[aa, cc]}, {{aa, 0}, {cc, 0}}]]
Evaluate[cc /. FindRoot[{xxinaTry[aa, bb] == bb xxoutaTry[aa, cc],
xxoutbTry[cc] == xxinbTry[aa, cc]}, {{aa, 0}, {cc, 0}}]]
In this case, I got the following kind of errors:
During evaluation of In[1]:= FindRoot::nlnum: The function value {0.244919 bb+xxinaTry[0.,bb],-0.244919} is not a list of numbers with dimensions {2} at {aa,cc} = {0.,0.}. >>
During evaluation of In[1]:= ReplaceAll::reps: {FindRoot[{xxinaTry[aa,bb]==bb xxoutaTry[aa,cc],xxoutbTry[cc]==xxinbTry[aa,cc]},{{aa,0},{cc,0}}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
During evaluation of In[1]:= NDSolve::dsvar: 0.12251026530612244` cannot be used as a variable. >>
During evaluation of In[1]:= ReplaceAll::reps: {NDSolve[{([Psi]^[Prime][Prime])[0.12251]==-0.8 [Psi][0.12251],([Psi]^[Prime])[0.1]==0,[Psi][1]==0.},[Psi],{0.12251,0.1,1},PrecisionGoal->20]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
During evaluation of In[1]:= FindRoot::nlnum: The function value {0.2 ([Psi]^[Prime])[1.],0.} is not a list of numbers with dimensions {2} at {aa,cc} = {0.,0.}. >>
...
It should be very complex. Could you provide some help? The above code can be run in Mathematica 9.0 and generate the same error in your computer, I believe.