Numerical approximation of the solution

AI Thread Summary
The discussion focuses on solving the boundary value problem defined by the differential equation u''(x) = f(x) with boundary conditions u(a) = 0 and u(b) = 0. The provided MATLAB code demonstrates how to implement a finite difference method to approximate the solution, specifically for the case where f(x) is a constant function. The user seeks assistance in modifying the existing MATLAB code to solve the equation u''(x) = sin(2πx) with the new boundary conditions u(-1) = 0 and u(1) = 0 over the interval [-1, 1]. The key points include the need to adjust the right-hand side of the equation to reflect the new function and to redefine the grid and matrix setup to accommodate the specified interval and boundary conditions.
splelvis
Messages
10
Reaction score
0
u''(x)=f(x),
boundary conditions u(a)=0,u(b)=0.
(u(x+h)-2u(x)+u(x-h))/h^2=f(x);

maltab code:

clear all
a=0;
b=1;
n=10;
h=(b-a)/(n+1);
x_with_boundary=linspace(a,b,n+2)';
x=x_with_boundary(2:n+1);
A=h^(-2).*(diag(ones(1,n-1),-1)+diag(-2.*ones(1,n),0)+diag(ones(1,n-1),1));
rhs=f4(x);
sol=A\rhs;
sol_with_boundary_conds=[0;sol;0];
plot(x_with_boundary,sol_with_boundary_conds);


open a new document,f4,
function y=f4(x)
y=ones(length(x),1);

parabola comes out.



now the question is ,

u''(x)=sin(2pix);
u(-1)=0;
u(1)=0;
interval[-1;1]

,how to change the MATLAB code to become u''=sin(2pix)?
thanks in advance
 
Physics news on Phys.org
anyone can help?
 

Similar threads

Replies
1
Views
2K
Replies
4
Views
3K
Replies
3
Views
4K
Replies
8
Views
3K
Replies
13
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top