- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the telephone icon in the upper-right corner of this page so we can investigate.
Asking for Matlab Code for boundary value problem using finite element method ?
7 views (last 30 days)
Show older comments
Let us consider the following problem:
I want to write Matlab code using finite element method in order to solve the above problem but I didn't succeed because am not familiar with that Matlab programming however I have tried to give such code below which it dosn't work ,any help ?
Here is my attempt. CODE
-u’’+6 u(x) = f(x), -1 <= x <=1, u(-1)=u(1)=0 %
% Input: x, Nodal points %
% Output: U, FE solution at nodal points %
% %
% Function needed: f(x)=(-4x^2-6)exp(x^2). %
% %
% Matlab functions used: %
% %
% hat1(x,x1,x2), hat function in [x1,x2] that is 1 at x2; and %
% 0 at x1. %
% %
% hat2(x,x1,x2), hat function in [x1,x2] that is 0 at x1; and %
% 1 at x1. %
% %
% int_hat1_f(x1,x2): Contribution to the load vector from hat1 %
% int_hat2_f(x1,x2): Contribution to the load vector from hat2 %
%
M = length(x);
for i=1:M-1,
h(i) = x(i+1)-x(i);
end
A = sparse(M,M); F=zeros(M,1); % Initialization
A(1,1) = 1; F(1)=0;
A(M,M) = 1; F(M)=0;
A(2,2) = 1/h(1); F(2) = int_hat1_f(x(1),x(2));
for i=2:M-2, % Assembling element by element
A(i,i) = A(i,i) + 1/h(i);
A(i,i+1) = A(i,i+1) - 1/h(i);
A(i+1,i) = A(i+1,i) - 1/h(i);
A(i+1,i+1) = A(i+1,i+1) + 1/h(i);
F(i) = F(i) + int_hat2_f(x(i),x(i+1));
F(i+1) = F(i+1) + int_hat1_f(x(i),x(i+1));
end
A(M-1,M-1) = A(M-1,M-1) + 1/h(M-1);
F(M-1) = F(M-1) + int_hat2_f(x(M-1),x(M));
U = A\F; % Solve the linear system of equations
return
[SL: formatted code as code]
1 Comment
Steven Lord
on 3 Sep 2020
What does "dosn't [sic] work" mean?
Also it will be impossible for us to try to run your code to investigate what's going on since you have not provided the int_hat1_f and int_hat2_f functions.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!