1
$\begingroup$

Let's define the following function:

f[t_, x_] = t^2 + t*x*(1 - x)

such that $x \in (0,1)$ and `$t \in (0,0.02)$ The problem is when I want to define the following function:

z[t_,x_] = Interpolation[Table[{x, f[t, x] Piecewise[{{1, 0.2 < x < 0.8}}]}, {t, 0,0.02},{x, 0,1, 1/24}]];

This is not working!! (I think the problem is because I discretize in space and not in time!!)

$\endgroup$
2
  • $\begingroup$ I'm not familiar with your goal but here are a couple of notes you probably missed: _ for arguments in defining f, using = instead of := in defining z, and also when you don't specify step in Table it's 1 which in your case t has almost no effect. Running Table alone will show you it will return a nested list not the kind of input Interpolation wants. Another thing is using similar names (z and Table). I think I should also remind using ClearAll before correcting the notes. $\endgroup$
    – Ben Izd
    Apr 1 at 11:44
  • $\begingroup$ You have right, the problem is that I get a list not the kind of input Interpolation wants. and I don't know how to solve this problem, I need a way to define a function of two parameters, t and x such that ${t, 0, 0.02}$ and ${x, 0,1, 1/24}$!! I don't know how!! $\endgroup$
    – walid fssm
    Apr 1 at 12:09

2 Answers 2

0
$\begingroup$

I'm not 100% clear what you are trying to achieve. If you want to sample your continuous function $f(t,x)$ at only discrete values of $x$, but be able to call it later for any value of $t$ then:

f[t_,x_]:= (t^2 + t x (1-x)) Piecewise[{{1,0.2<x<0.8}}];

which we can sample then using:

zdis[t_]:= Table[f[t,x],{x,0,1,1/24}];

You can plot all values then using:

Plot[Evaluate[zdis[t]],{t,0,0.04},PlotLabels-> Table[InputForm[x], {x, 0, 1, 1/24}]]

giving:

Plots of zdis[t]

Addendum:

To get the $i$th value where $x = i/24$ for $i\in [0,24]$ you can either just call

zdis[t][[i+1]]

or build this into the original definition as per:

zdis[t_,i_?IntegerQ]:= f[t,i/24];

where the ?IntegerQ requires the input to be an integer.

$\endgroup$
2
  • $\begingroup$ thank you, this is exactly what I'm looking for, I just need a little more detail please. after you define the table $zdis[t_]$, can you please show me how I can define a new function $g(t , i)$ such that for each value of $ t \in (0,0.02)$ and $i \in (0,1/24,1/12,....,1)$ I get the value !! $\endgroup$
    – walid fssm
    Apr 1 at 17:58
  • $\begingroup$ See the addendum to the answer. Note that the $i$ input in my answer is an integer, not a multiple of 1/24, which is harder to do (and unnecessay). Defining this secondary function zdis is not really necessary in the first place if you only ever call f[t,x] with values of $x \in [0,1/24,2/24,....]$. $\endgroup$ Apr 1 at 18:38
0
$\begingroup$

Perhaps FunctionInterpolationis what you're looking for.

Try

f[t_, x_] = t^2 + t*x*(1 - x)
fip = FunctionInterpolation[f[t, x] Piecewise[{{1, 0.2 < x < 0.8}}], {t, 0, 0.02}, {x, 0, 1 }] 
Plot3D[fip[t, x], {t, 0, 0.02}, {x, 0, 1}]

enter image description here

Addendum

For discrete interpolation in x we transform f[t,x] in a product {t^2,t}. {1,x(1-x).

Now it's possible to interpolate x:

ip = Interpolation[Table[{x, Piecewise[{{1, 0.2 < x < 0.8}}] {1, x (1 - x)}}, {x, 0, 1,1/24}],InterpolationOrder->1];

Plot[ip[x],{x,0,1}]

enter image description here

Final function follows to {t^2,t}.ip[x]!

$\endgroup$
5
  • $\begingroup$ Thank you for your answer, But I see that you consider x in the whole interval (0,1). Me I need $(x, 0,1, 1/24)$. $\endgroup$
    – walid fssm
    Apr 1 at 12:13
  • $\begingroup$ That means you're looking for a discrete interpolation ,{x, 0,1, 1/24}? $\endgroup$ Apr 1 at 12:21
  • $\begingroup$ that's right, I'm looking to define a discrete function in space but not in time.!! $\endgroup$
    – walid fssm
    Apr 1 at 12:24
  • $\begingroup$ @walidfssm See my modified answer, now with discrete interpolation x. $\endgroup$ Apr 1 at 13:20
  • $\begingroup$ Thank you for your answer, But, this is not what I need. is it not possible to define it without separating it into a product!! $\endgroup$
    – walid fssm
    Apr 1 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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