Having all the codes in a single file

조회 수: 2 (최근 30일)
Pouyan Msgn
Pouyan Msgn 2021년 3월 28일
댓글: Jan 2021년 3월 29일
I saw an example of boundary value problems in the book Numerical Analysis by Sauer. We have the non-linear equation:
The discretized form of this equation :
My question is not about how we solve such a problem. I already have the solution in three files. Ie two functions and a regular matlab file to perform it. I also attached files.I tried to write everything in a single file and wanted to see if it is possible to execute the algorithm without having to write Matlab functions. I did not succeed. Can anyone correct my code?
clc
clear all
n=15;
a=0; b=1; ya=1; yb=4;
h=(b-a)/(n+1);
J=zeros(n,n); %Jacobian
w=zeros(n,1);
y=zeros(n,1);
y(1)=ya-(2+h^2)*w(1)+h^2*w(1)^2+w(2); %BV
y(n)=w(n-1)-(2+h^2)*w(n)+(h^2)*(w(n)^2)+yb; %BV
for i=2:n-1
y(i)=w(i-1)-(2+h^2)*w(i)+(h^2)*(w(i)^2)+w(i+1);
end
for i=1:n-1
J(i,i+1)=1;
J(i+1,i)=1;
end
%The Matrix
for i=1:n
J(i,i)=2*h^2*w(i)-2-h^2;
end
%The solution!
w=w-J\y;
Y=[1;w;4];
plot(x,Y)
grid on
This is the True solution! but I dont get this plot!
  댓글 수: 3
Pouyan Msgn
Pouyan Msgn 2021년 3월 28일
I dont get the correct plot with my own code
Jan
Jan 2021년 3월 29일
I do not get any output from your code at all, because x is not defined in the command plot(x,Y).

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by