0
$\begingroup$

I have two functions T1(x) and T2(x). My objective is to output the result of both as .csv files and then take the average.

The x range for both the functions is same but mathematica evaluates each of these functions at different number of points. How can I force mathematica to allow evaluation at same number of points for both the functions ?

The code for the first function is:

ww = 0.75 10^-3; wc = 
 0.25 10^-3; d = 0.002; L = 0.0252; u = 0.0133; cp = 4178; ks = 390; \
tfi = 300; rho = 996.497; h = 4683; 
m = Sqrt[(2*h)/(ks*ww)]; α = (2*h)/(cp*rho*u*wc); β = (2*h)/(ks*ww); p = Sqrt[α^2 + 4*β]; eta =Tanh[d*m]/(d*m); σ = d/wc;
γ = 26455.03/ks; 

Tw0[x_, y_] = (((α*γ)/E^(0.5*x*(α + p)))*((α - p)*E^(0.5*L*(α + p)) + E^(L*p)*(p - α) + 2*(α + β*x)*E^(L*p + 0.5*x*(α + p)) + (α + p)*E^(p*x)*(1 - E^(0.5*L*(α + p))) - 2*E^(0.5*x*(α + p))*(α + β*x)))/(2*β^2*d*(E^(L*p) - 1)) + (γ*Cosh[m*(d - y)])/(m*Sinh[d*m])  ; 

eqn = Derivative[3][y][x] == -((2*α*β*γ*d)/((d*m)^2 + (Pi*n)^2)) + (β + ((Pi*n)/d)^2)*Derivative[1][y][x] + α*((Pi*n)/d)^2*y[x] - α*Derivative[2][y][x]; 
eq = DSolve[{eqn, Derivative[1][y][0] == 0, Derivative[1][y][L] == 0, Derivative[2][y][0] - y[0]*(β + ((Pi*n)/d)^2) == 0}, y[x], x]; 
sol = eq[[1]];
sol = y[x] /. sol;
sol = sol*Cos[(Pi*n*y)/d]; 
Twn = Sum[sol, {n, 1, 10}];

Tsw[x_, y_] = Tw0[x, y] + Twn;

T1[x_] = Integrate[Tsw[x, y]/d, {y, 0, d}];

T1 = Cases[Plot[{T1[x]}, {x, 0, L}], Line[data_] :> data, -4, 1][[1]];

Export["T1.csv", T1]

The second function T2(x) is defined using the following code (Please note that I used two different kernels to evaluate each function, in order to avoid variable name overlap)

T2[x_]=(1/(2 (-1 + E^(L Sqrt[α^2 + 4 β])) β^2 Sqrt[α^2 + 4 β]))E^(-L α - 1/2 x (3 α + Sqrt[α^2 + 4 β])) P (-2 E^(L α + 1/2 x (3 α + Sqrt[α^2 + 4 β]))Sqrt[α^2 + 4 β] (α^2 + β + x α β) + 2 E^(L (α + Sqrt[α^2 + 4 β]) + 1/2 x (3 α + Sqrt[α^2 + 4 β]))Sqrt[α^2 + 4 β] (α^2 + β + x α β) + E^(x α + L (α + Sqrt[α^2 + 4 β])) α (α^2 + 4 β - α Sqrt[α^2 + 4 β]) - E^(x α + 1/2 L (3 α + Sqrt[α^2 + 4 β])) α (α^2 + 4 β - α Sqrt[α^2 + 4 β]) + E^(L α + x (α + Sqrt[α^2 + 4 β])) α (α^2 + 4 β + α Sqrt[α^2 + 4 β]) - E^(x (α + Sqrt[α^2 + 4 β]) + 1/2 L (3 α + Sqrt[α^2 + 4 β])) α (α^2 + 4 β + α Sqrt[α^2 + 4 β]))

β = 4002.564 
α = 42.265
P = 67833.40
L = 0.0252

T2 = Cases[Plot[{T2[x]}, {x, 0, L}], Line[data_] :> data, -4, 1][[1]];

Export["T2.csv", T2]
$\endgroup$
2
  • 1
    $\begingroup$ I am guessing if you replace T1=Cases[Plot[{T1[x]},{x,0,L}]... and T2=Cases[Plot[{T2[x]},{x,0,L}]... with t1=Table[{x,T1[x]},{x,0,L,L/100}];T1=Cases[ListPlot[t1,Joined->True]... and t2=Table[{x,T2[x]},{x,0,L,L/100}];T2=Cases[ListPlot[t2,Joined->True]... that it will evaluate T1 and T2 at exactly the same horizontal coordinates and draw lines between those points as needed to be extracted by your code. You can change the L/100 to a different step size as needed. Please test this carefully and verify that it works exactly correctly. $\endgroup$
    – Bill
    Aug 21, 2020 at 18:02
  • $\begingroup$ @Bill Works perfectly ! If you write this as an answer, I will be happy to accept it. Much appreciated. $\endgroup$
    – Avrana
    Aug 21, 2020 at 18:13

1 Answer 1

3
$\begingroup$

Plot is "smart" and "adaptive" and the points at which it chooses to evaluate the function depend on the function. You can force your own evaluation points by using Table and ListPlot.

Replace

T1=Cases[Plot[{T1[x]},{x,0,L}]...
T2=Cases[Plot[{T2[x]},{x,0,L}]...

with

t1=Table[{x,T1[x]},{x,0,L,L/100}];
T1=Cases[ListPlot[t1,Joined->True]...
t2=Table[{x,T2[x]},{x,0,L,L/100}];
T2=Cases[ListPlot[t2,Joined->True]...

and that will force it to evaluate T1 and T2 at exactly the same horizontal coordinates and draw lines between those points as needed to be extracted by your code. You can change the L/100 to a different step size as needed.

You might also consider using different names for

T1[x_]=Integrate[...

and

T1=Cases[ListPlot[...

also

T2[x_]=Integrate[...

and

T2=Cases[ListPlot[...

Using the same name for a function and then for data worries me.

Please test this carefully and verify that it works exactly correctly.

$\endgroup$
1
  • $\begingroup$ Isn't the answer simply Export["t2.csv", t1] and Export["T2.csv", t2], skipping the plots altogether? $\endgroup$
    – Michael E2
    Aug 23, 2020 at 16:24

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.