Having all the codes in a single file
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I saw an example of boundary value problems in the book Numerical Analysis by Sauer. We have the non-linear equation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/565136/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/565141/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/565146/image.png)
The discretized form of this equation :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/565151/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/565156/image.jpeg)
This is the True solution! but I dont get this plot!
3 Commenti
Jan
il 29 Mar 2021
I do not get any output from your code at all, because x is not defined in the command plot(x,Y).
Risposte (0)
Vedere anche
Categorie
Scopri di più su Calculus in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!