Having all the codes in a single file

2 Ansichten (letzte 30 Tage)
Pouyan Msgn
Pouyan Msgn am 28 Mär. 2021
Kommentiert: Jan am 29 Mär. 2021
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 Kommentare
Pouyan Msgn
Pouyan Msgn am 28 Mär. 2021
I dont get the correct plot with my own code
Jan
Jan am 29 Mär. 2021
I do not get any output from your code at all, because x is not defined in the command plot(x,Y).

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by