MATLAB: Construction of diagonal matrix of functions

matrix

I have three functions: f_1, f_2, and f_3.
I want to construct the matrix A for the following linear system:
so the first line of the system will be f_2(x_1) + f_3(x_2) = q_1
second line will be f_1(x_1) + f_2(x_2) + f_3(x_3) = q_2
third line f_1(x_2) + f_2(x_3) + f_3(x_4) = q_3
and so on.
Thank you.

Best Answer

This might be what you want. It assumes that f_1,2,3(x) work element-wise.
function A=func(x)
f1=f_1(x(1:end-1));
f2=f_2(x);
f3=f_3(x(2:end));
A=diag(f1,-1)+diag(f2)+diag(f3,+1);
end