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