If you want to solve the Fredholm equation of the second kind which is an integral equation of the form
$$f(x) - \lambda\int_{a}^{b} K(x,y)f(y)dy = g(x), \quad \forall x \in [a, b]$$
you can use the following code. We follow the simplistic source. One can make it much better by considering further implementation aspects as well as injecting more state of the art algorithmic antibiotic!!
Code:
Options[FredholmKind2] = {Method -> Automatic};
FredholmKind2[{a_, b_, lambda_, k_, g_}, n_?IntegerQ,OptionsPattern[]] :=
Block[{step, SI, GI, KMatrix, W, DMatrix, f, deltaX, delta},
step = (b - a)/n;
SI = Range[a, b, step];
GI = g /@ SI;
KMatrix = Outer[k, SI, SI];
W = {step/2}~Join~ConstantArray[step, n - 1]~Join~{step/2};
DMatrix = DiagonalMatrix[W];
f = If[OptionValue[Method] === NIntegrate,
deltaX[x_?NumericQ] := W . (k[x, #] & /@ SI) -
NIntegrate[k[x, y], {y, a, b},AccuracyGoal -> 4];
(*If the integral is expensive ParallelMap is an option here *)
delta = deltaX /@ SI;
Interpolation[
Transpose@{SI,
LinearSolve[IdentityMatrix[n + 1] + lambda*(DiagonalMatrix[delta] -
KMatrix . DMatrix), GI]}]
,
Interpolation[
Transpose@{SI,
LinearSolve[IdentityMatrix[n + 1] - lambda*(KMatrix . DMatrix),GI]}]
];
f
]
Testing:
Now lets test it for the following equation which has an exact solution Sin[x]
!!
$$f(x) - \frac{4}{\pi}\int_{0}^{\frac{\pi}{2}} \cos(x-y)f(y)dy = -\frac{2}{\pi}\cos(x), \quad \forall x \in \left[0, \frac{\pi}{2}\right]$$
There are two separate methods available called Automatic
and NIntegrate
. The second method is computationally more expensive but produces better result. The above function returns the solution f
as an InterpolatingFunction
which you can later use an ordinary function in MMA.
n=90;(*number of discretization*)
a = 0.;
b = 0.5*Pi;
lambda = 4./Pi;
Kpart[x_, y_] := Cos[x - y];
Gpart[x_] := -2. Cos[x]/Pi;
f1 = FredholmKind2[{a,b,lambda,Kpart, Gpart},n,Method -> Automatic];
f2 = FredholmKind2[{a,b,lambda,Kpart, Gpart},n,Method -> NIntegrate];
Needs["PlotLegends`"];
Plot[Evaluate@Sqrt@((Sin[x] - #)^2 & /@ {f1[x], f2[x]}), {x, a, b},Frame -> True,
Axes -> False,PlotLegend -> {"Automatic", "NIntegrate"},LegendPosition -> {1.1, -0.4}]

In the above plot one can see how accurate the numerical solutions perform w.r.t the exact solution.
To Do!
Now if you want to solve the Fredholm integral equation of the first kind which looks like
$$\int_{a}^{b} K(x,y) f(y) dy = g(x), \quad \forall x \in [a,b],$$
I suggest that you take look at this at page 213. After the above example it will be easy for you to implement the solution for the problem of first kind.
BR
NDSolve[]
is meant for differential equations, and there isn't a built-in function (yet) for solving integral equations. OTOH, solutions to an inhomogeneous Fredholm equation of the second kind, like in your example, can be solved with the Liouville-Neumann series; such an expansion ought to be doable with the built-in functions of Mathematica. $\endgroup$f[x] == Integrate[f[x] g[x], x]
(thus, your "a bit more complex" is one hell of an understatement). This is a nonlinear problem, whose solution is much more difficult... $\endgroup$