Hello, i have to solve this with a method of finite differences
x'' = -2x'(t) – 2x(t) + e^(-t) + sin(2t)
t ϵ [0,4]; x(0) = 0,6; x(4) = -0,1
And here is my code in Matlab
clear all;
al=0.6; bt=-0.1; h=0.01;
t=0:h:4;
n=length(t)-2;
for i=1:n
if i~=1, a(i,i-1)=-1+h;end
a(i,i)=2-2*(h^2);
if i~=n, a(i,i+1)=-1-h;end
f(i)=-(h^2)*(exp(-t))-(h^2)*(sin(2*t(i)));
end
f(1)=f(1)-al*(-1+h);
f(n)=f(n)-bt*(-1+h);
x=a\f';
x1=[al,y',bt];
figure(1),plot(t,x1)
when i run this i get this error: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side. Error in differences (line 9) f(i)=-(h^2)*(exp(-t))-(h^2)*(sin(2*t(i)));
And is my solution of the task without matlab:
x'' + 2x'(t) + 2x(t) = e^(-t) + sin(2t)
- a = 1; b = 2; c=2; d = e^(-t) + sin(2t)
Ai,i-1 = -ai + (h/2)bi =-1 + (h/2).2=-1+h
Ai,i = 2ai – h2ci = 2.1 – 2h^2 = 2 - 2h^2
Ai,i+1 = -ai - (h/2)bi = -1 – (h/2)2 = -1 - h
fi = -h^2di = -h^2(e^(-t)+ sin(2t))
so my questions are why im getting this error and are my coeficients right.
Thanks in advance. :)