Impossible manipulation

Hi everybody, I can’t solve a following problem. There is one formula:

Q = a * (AX - b) + BX * c + d * CX + f * (DX - FX),

where a, b, c, d, f, g - constants, which values are unknown, but areas of values (from-to) are defined, so I could manipulate their;
AX, BX, CX, DX, EX - known lists of data;
FX - a list of data, which values are also unknown: FX[i + 1] = FX[ i ] + 2 * (f / g) * (EX[[ i ]] - FX[ i ]), FX[1]=EX[[1]];
My target: to get Q through manipulate of constants (fit with another graphic).
The problem: as long as FX is not found Mathematica can’t manipulate constants.

I would be glade your help and ideas…

Here I have an error message (see also attachments)

Manipulate[FX[1] = EX[[1]]; 
 FX = Table[
   FX[i + 1] = FX[i] + 2 (f/g)*(EX[[i]] - FX[i]), {i, 1, Length[EX]}];
  ListLinePlot[a*(AX - b) + c*BX + d*CX + f*(DX - FX), Frame -> True, 
  PlotRange -> All, 
  FrameLabel -> {Style[Time [s], 12, Bold], 
    Style[TotalPower [W], 12, Bold]}, ImageSize -> 1000], {{g, 5}, 5, 
  700, 1}, {{f, 1}, 1, 20, 0.01}, {{a, 0}, 0, 500, 1}, {{c, 0}, 0, 5, 
  0.01}, {{b, 0}, -0.1, 0.1, 0.001}, {{d, 400}, 400, 800, 5}]

And here the proces hangs

FX[1] = EX[[1]]; FX = 
 Table[FX[i + 1] = FX[i] + 2 (f/g)*(EX[[i]] - FX[i]), {i, 1, 
   Length[EX]}]

I think your construction of FX ist not the real thing. What about ( with EX = {3, 6, 4, 8} for example )

FX[1] = EX[[1]];
FX[i_] := FX[i - 1] + 2 f/g (EX[[i - 1]] - FX[i - 1])

Which yields

In[10]:= FFX = 
 Table[FX[i + 1] = FX[i] + 2 (f/g)*(EX[[i]] - FX[i]), {i, 1, Length[EX]}] // Simplify


Out[10]= {3, 3 + (6 f)/g, 3 - (12 f^2)/g^2 + (8 f)/g,  3 + (24 f^3)/g^3 - (28 f^2)/g^2 + (18 f)/g}

and try your ListlinePlot with FFX (but don’t use the definitions of FX in the Manipulate Object)

Hey Hans, thank you so much for your quick reply!
You’re right - it works if “i” is not so large. But in my case “i” equates about 20000 and Mathematica is always running…

hmmmm.

perhaps you can construct a function of a, b, … for each i.

a * (AX - b) + BX * c + d * CX is I think sort of linear in a, b, c, …

Now look af FFX. Each component seems to be a polynomial in f / g. perhaps one can figure out a recursion relation for ( f / g )^j for each j. In this case you could avoid the really big tables…?

It’s a nice idea. But how do I define j?

Good morning Leo (here it is about 11 am), perhaps this will help you a bit:

In[21]:= RSolve[{F1X[i] ==    F1X[i - 1] + 2 f/g (E1X[i - 1] - F1X[i - 1]), F1X[1] == E1},  F1X[i], i]

To get rid of the somewhat annyoing K[1]

RSolve[{F1X[i] == F1X[i - 1] + 2 f/g (E1X[i - 1] - F1X[i - 1]),    F1X[1] == E1}, F1X[i], i] /. K[1] -> j

and even a bit more simplified

RSolve[{F1X[i] == F1X[i - 1] + 2 f/g (E1X[i - 1] - F1X[i - 1]),    F1X[1] == E1}, F1X[i], i] //. {-g Sum[XX_, YY_] :> Sum[g XX, YY], 
  K[1] -> j}

Good morning Hans (I am from Germany too), thank you for your help. RSolve and even RecurrenceTable doesn’t work (error; s. Attachments). If I use RecurrenceTable for Length about 20000 an error message arises.

I would use FoldList to build your FX. For vectors of length 1000 the following is not too slow:

With[{n = 1000},
 EX = RandomReal[{0, 1}, n];
 AX = RandomReal[{0, 1}, n];
 BX = RandomReal[{0, 1}, n];
 CX = RandomReal[{0, 1}, n];
 DX = RandomReal[{0, 1}, n];
 FX[f_, g_] = 
  Most@FoldList[Expand[#1 + 2 (f/g)*(#2 - #1)] &, EX[[1]], EX];
 Manipulate[
  ListLinePlot[a*(AX - b) + c*BX + d*CX + f*(DX - FX[f, g]), 
   Frame -> True, PlotRange -> All, ImageSize -> 1000], {{g, 5}, 5, 
   700, 1}, {{f, 1}, 1, 20, 0.01}, {{a, 0}, 0, 500, 1}, {{c, 0}, 0, 5,
    0.01}, {{b, 0}, -0.1, 0.1, 0.001}, {{d, 400}, 400, 800, 5}]]

Thanks for your interesting variant. It really works for n=1000, but for 15000-20000 it’s running until now…

I’ve just noted (f/g) is marked with dark blue and not light blue as all constants. So if I give values for f and g it works. But I want to manipulate these parameters.

This variant with delayed definition seems faster:

With[{n = 20000}, EX = RandomReal[{0, 1}, n];
 AX = RandomReal[{0, 1}, n];
 BX = RandomReal[{0, 1}, n];
 CX = RandomReal[{0, 1}, n];
 DX = RandomReal[{0, 1}, n];
 FX[f_, g_] := Most@FoldList[#1 + 2 (f/g)*(#2 - #1) &, EX[[1]], EX];
 Manipulate[
  ListLinePlot[a*(AX - b) + c*BX + d*CX + f*(DX - FX[f, g]), 
   Frame -> True, PlotRange -> All, ImageSize -> 1000], {{g, 5}, 5, 
   700, 1}, {{f, 1}, 1, 20, 0.01}, {{a, 0.}, 0, 500, 1}, {{c, 0.}, 0, 
   5, 0.01}, {{b, 0.}, -0.1, 0.1, 0.001}, {{d, 400.}, 400, 800, 5}]]

Check out my use of Most, which may not be what you mean.

Gianluca, I thank you very much, all funktionetes!

Guten Abend Leo (ich bin auch aus D),

perhaps this may be helpful as well. At the end of thenotebook attached you find a function which gives the n-th Element of Vector Q.

The main problem seems to be that you have a sum over n elements of vector EF, which could get lengthy for big n. Perhaps one could speed that up by compiling.

Some remarks. It sounds that you try to fit your parameters a, b, …to experimental data Q. What about for a first step extracting k times 6 Q’s and using FindRoot to Solve for a,…,f. for each of these data extracted (form different regions of Q)? That could give a rough idea in which region of the parameters you should look. Or extracting randomly a couple of 100 data and use Fit. With fQ you now have a Function which could be fitted…

Grüße Hans

Hallo Hans,

I thank you so much for making of generalization about my problem and also for time and energy which you spent for that. You showed me so much important things and I’ll add they to my armoury.

It seems to be right that problem is n-elements in data EX less than this one in FX. That’s why several logical things (also the last - vector) in this situation don’t work.

A little bit about my aim here. Q is in contrast to EX not experimental data, must be calculated. And I unfortunatelly can’t work with different regions of experimental data because on graphic for search Q through manipulation of constants I need to see the picture with all data to fit the line.

Thanks one more time for help!

Guten Abend Leo. Was genau ist denn das Problem? An was soll Q gefitted werden?

Gruß Hans

Guten Abend Hans. Es gibt eine ideale Kurve, die mir bekannt ist. Q ist eine zeitlich abhängige Kurve,die ich aus experimentellen Daten sowie durch Manipulation der Konstanten bekomme. Aber nun klappt es mithilfe der Methode von Gianluca. Ich bedanke mich bei Ihnen noch mal.

I am glad to be helpful. Did you find the right way to shorten the vector of length n+1?

No, I didn’t. But your method works!