function rmserror=nma_fem(a,b,c,f,x0,u0,beta,len,exact_sol,nNodes) % Solves numerically a second order ODE with constant coeff. % using the symmetric Garlekin Finite Elements Methods. % % see the file nma_fem_driver.m on how to call this function. % % Solve a u''(t) + b u'(t) + c u(t) = f(t) % % with t over the range t0 to len % and with initial conditions u(0)=u0 % and with u'(len)=beta %by Nasser Abbasi. Sept 26,2006. xc=linspace(x0,len,nNodes); % This plots the shape functions. % x=linspace(x0,len,1000); % y=linspace(x0,len,1000); % for i=1:nNodes % for j=1:length(y) % [v,d]=phi(i,x(j),nNodes,x0,len,xc); % y(j)=v; % end % plot(x,y); % hold on; % end A = build_stiffness_matrix(nNodes,xc,a,b,c); load = build_load_vector(a,u0,nNodes,xc,f,beta); % Now remove the first row and column from the stiffness matrix A(2,1) = A(2,1)*u0; load(2) = load(2)-A(2,1); A = A(2:end,2:end); load = load(2:end); % SOLVE for unknowns q = A\load; q = [u0;q]; % Plot the solution y = zeros(length(xc),1); for i=1:length(xc) y(i)=trial(xc(i),x0,len,xc,nNodes,q); end plot(xc,y,'ro'); hold on; line(xc,y); % Calculate RMSerror. Use 50 points. Should be more than enough. NPOINTS = 50; x = linspace(x0,len,NPOINTS); rmserror = 0; for i = 1:length(x) y = trial(x(i),x0,len,xc,nNodes,q); t=x(i); % USED for subs below. do not remove. yexact = real(double(subs(exact_sol))); rmserror = rmserror+(y-yexact)^2; end rmserror = rmserror/NPOINTS; rmserror = sqrt(rmserror); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function v = trial(x,x0,len,xc,nNodes,q) if xlen error('in Trial. x outside range'); end v = 0; for i = 1:nNodes [s,d] = phi(i,x,nNodes,x0,len,xc); %notice ignore d here. v = v+ q(i)*s; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [v,d]=phi(i,x,nNodes,x0,len,xc) if(i<1 || i>nNodes) error('node number outside range'); end if(xlen) error('x outside range'); end h = xc(2)-xc(1); if i==1 if(x>xc(2)) v = 0; d = 0; else v = (xc(2)-x)/h; d = -1/h; end return; end if i==nNodes if(xxc(i+1) || x