Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Matlab question in French. Please check my code and helo with the Picture with the question. i am not able to make it work %

Matlab question in French. Please check my code and helo with the Picture with the question. i am not able to make it work
% Function definitions
P = @(x)-1./ x; % Definition of p(x)
Q = @(x)0* x; % Definition of q(x)
R = @(x)-1.6./(x.^4); % Definition of r(x)
a =0.9; b =1; alpha =0; beta =1;
% Parameters
h1=0.01;
h2=0.005;
N1= round((b - a)/ h1-1);
N2= round((b - a)/ h2-1);
x1= linspace(a + h1, b - h1, N1);
x2= linspace(a + h2, b - h2, N2);
P1= P(x1);
Q1= Q(x1);
R1= R(x1);
P2= P(x2);
Q2= Q(x2);
R2= R(x2);
% Vrification des tailles
disp('Tailles de P1, Q1, R1 :');
disp(size(P1));
disp(size(Q1));
disp(size(R1));
disp('Tailles de P2, Q2, R2 :');
disp(size(P2));
disp(size(Q2));
disp(size(R2));
% Compute solutions
y1= problimite(N1, P1, Q1, R1, a, b, alpha, beta);
y2= problimite(N2, P2, Q2, R2, a, b, alpha, beta);
% Exact solution
c =1.4; d =0.81;
x_exact = linspace(a, b,50);
y_exact =(c -0.4./ x_exact.^2)-(c -0.4/ d)*(log(x_exact)/ log(0.9));
% Plot
plot(x_exact, y_exact, '--', 'LineWidth', 2);
hold on;
plot(x1, y1, 'LineWidth', 2);
plot(x2, y2, 'LineWidth', 2);
legend('Exact solution', 'h =0.01','h =0.005');
xlabel('x');
ylabel('y');
title('Approximations numriques vs Solution exacte');
grid on;
hold off;
% Calculation of error
Nbr_pt =600;
Erreur_h = zeros(1, Nbr_pt);
for ii =1:Nbr_pt
N = ii;
h_i =(b - a)/(ii +1);
h_valeurs(ii)= h_i;
x_i = linspace(a + h_i, b - h_i, N);
x_i_inter = x_i(2:end -1);
P_err = P(x_i_inter);
Q_err = Q(x_i_inter);
R_err = R(x_i_inter);
y_boucle = problimite(N, P_err, Q_err, R_err, a, b, alpha, beta);
y_boucle = y_boucle';
y_exacte_i =(1.4-0.4./ x_i_inter.^2)-(1.4-0.4/0.81)*(log(x_i_inter)/ log(0.9));
erreur = abs(y_boucle - y_exacte_i);
Erreur_h(ii)= max(erreur);
end
% Plot of the error
figure;
loglog(h_valeurs, Erreur_h, 'LineWidth', 2);
xlabel('h');
ylabel('Erreur');
title('Erreur en fonction de la taille du pas (h)');
grid on;
function x = tridiagonal(D, L, U, B)
% Get the length of the main diagonal
n = length(D);
% Construct the sparse tridiagonal matrix A
A = spdiags([L D U],-1:1, n, n);
% Solve the system Ax = B
tic; % Start timer
x = A \ B;
toc; % End timer and display elapsed time
end
function y = problimite(N, P, Q, R, a, b, alpha, beta)
% Initialize vectors for the tridiagonal system
D = zeros(N,1);
L = zeros(N -1,1);
U = zeros(N -1,1);
B = zeros(N,1);
% Fill the vectors based on P, Q, R, and other parameters
for i =1:N
D(i)= P(i)+ Q(i)+ R(i);
B(i)= a * i^2+ b * i;
if i >1
L(i -1)= P(i)- Q(i -1);
end
if i N
U(i)= P(i)- Q(i);
end
end
% Solve the tridiagonal system
y_interior = tridiagonal(D, L, U, B);
% Construct the complete solution vector
y =[alpha; y_interior; beta];
end
it shows lik that :
Classroom License -- for classroom instructional use only.
>> aaaaa
Tailles de P1, Q1, R1 :
19
19
19
Tailles de P2, Q2, R2 :
119
119
119
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in aaaaa>tridiagonal (line 96)
A = spdiags([L D U],-1:1, n, n);
Error in aaaaa>problimite (line 124)
y_interior = tridiagonal(D, L, U, B);
Error in aaaaa (line 40)
y1= problimite(N1, P1, Q1, R1, a, b, alpha, beta);
>>
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago