0
$\begingroup$

This is my code, i want to solve system by 14 equations and 14 unknowns(rr list).But the code doesn't run...

Clear["Global`*"]
T[0, t_] = 1;
T[1, t_] = t;
T[n_, t_] := 2 *t*T[n - 1, t] - T[n - 2, t];
For[n = 0, n <= 7, n++, Print["T[", n, ",t]= ", T[n, t], "\n"]];
tableoft = tt /.NSolve[T[7, tt], tt];
tableoft[[1]];
Subscript[z, 1][t_] = Sum[Simplify[Subscript[a, j]*T[j, t]], {j, 0, 6}];
Subscript[z, 2][t_] = Sum[Simplify[Subscript[b, l]*T[l, t]], {l, 0, 6}];
f[t_] = (t^4/6 - t^3/3 + t) /. t -> 1/2*(τ + 1);
Subscript[k, 1][t_, s_] = s^3 /. s -> 1/4*(τ + 1)*(r + 1);
Subscript[k, 1][t, s]*Subscript[z, 1][r];
p[τ_] = 
 Integrate[Subscript[k, 1][t, s]*Subscript[z, 1][r], {r, -1, 1}]
Subscript[k, 2][t_, s_] = -2 (t - s) /. t -> 1/2*(τ + 1) /. 
  s -> 1/4*(τ + 1)*(r + 1);
Subscript[k, 2][t, s]*Subscript[z, 2][r];
    pp[τ_] = 
 Integrate[Subscript[k, 2][t, s]*Subscript[z, 2][r], {r, -1, 1}];
Subscript[g, 2][τ_] = 
 Expand[(f[t] + p[τ] + pp[τ])^2] // N;
Subscript[δ, 
  1][τ_] = ((Subscript[z, 
    1][τ])*((f[t] + p[τ] + pp[τ])) - 1) // N;
Subscript[δ, 
  2][τ_] = (Subscript[z, 2][τ] -Subscript[g, 2][τ]) // N;
 Table[N[Subscript[δ, 1][tableoft[[i]]]], {i, 1, 7}];
 Table[N[Subscript[δ, 2][tableoft[[i]]]], {i, 1, 7}];
 r = Flatten[Table[N[{Subscript[δ, 1][tableoft[[i]]], 
 Subscript[δ, 2][tableoft[[i]]]}], {i, 1, 7}]];
 rr =Table[Simplify[r[[i]]] == 0, {i, 1, 14}]
 list =Flatten[Table[{Subscript[a, i], Subscript[b, i]}, {i, 0, 6}]];
 Solve[rr, list]

How to get solution fast?

$\endgroup$
3
$\begingroup$

You could use FindRoot like so:

sol = FindRoot[rr, Thread[{list, ConstantArray[0, Length[list]]}]]

(* {Subscript[a, 0] -> 18.9824, Subscript[b, 0] -> 0.43251, 
 Subscript[a, 1] -> -33.3436, Subscript[b, 1] -> 0.65013, 
 Subscript[a, 2] -> 27.5785, Subscript[b, 2] -> 0.279464, 
 Subscript[a, 3] -> -22.0304, Subscript[b, 3] -> 0.0771869, 
 Subscript[a, 4] -> 16.5283, Subscript[b, 4] -> 0.0177385, 
 Subscript[a, 5] -> -11.0201, Subscript[b, 5] -> 0.00270039, 
 Subscript[a, 6] -> 5.51, Subscript[b, 6] -> 0.000329395} *)

Another way is to remove the Solve and instead do a minimization. Transform the equations lhs == rhs into lhs - rhs == 0 if they aren't like this already, then minimize the total square error from zero:

{err, sol} = NMinimize[Total[(rr /. Equal -> Subtract)^2], list]

(* {1.00027*10^-26, {Subscript[a, 0] -> 18.9824, 
  Subscript[b, 0] -> 0.43251, Subscript[a, 1] -> -33.3436, 
  Subscript[b, 1] -> 0.65013, Subscript[a, 2] -> 27.5785, 
  Subscript[b, 2] -> 0.279464, Subscript[a, 3] -> -22.0304, 
  Subscript[b, 3] -> 0.0771869, Subscript[a, 4] -> 16.5283, 
  Subscript[b, 4] -> 0.0177385, Subscript[a, 5] -> -11.0201, 
  Subscript[b, 5] -> 0.00270039, Subscript[a, 6] -> 5.51, 
  Subscript[b, 6] -> 0.000329395}} *)

rr[[All, 1]] /. sol

(* {0., -3.46945*10^-18, 2.22045*10^-16, -2.34188*10^-16, 2.22045*10^-15,
  5.55112*10^-17, -1.66533*10^-15, 1.77636*10^-15, -4.32987*10^-15, 
 3.55271*10^-15, -1.9984*10^-15, 0., -5.88418*10^-15, -8.52651*10^-14} *)

As you can see, substituting these values satisfies all equations at an error under 10^-13.

$\endgroup$

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.