Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I translate this to C++? clc clear P0 = @(x) 1; P1 = @(x) x; for i = 0:4;

ID: 3543507 • Letter: H

Question

How do I translate this to C++?

clc

clear

P0 = @(x) 1;

P1 = @(x) x;


for i = 0:4;

P = @(x) ((2*i+3)/(i+2)).*x.*P1(x) - ((i+1)/(i+2))*P0(x);

P0 = P1;

P1 = P;

end

P6 = P;

dx = 0.0001;

dP6 = @(x) (P6(x+dx)-P6(x))/dx; % differentiation of P6(x)


x0 = [-0.9,-0.7,-0.3,0.3,0.7,0.9]; %six initial guess because there are 6 solutions

sol = zeros(size(x0));

for i = 1:length(x0);

x = x0(i);

cnt = 1;

err = 1;

while err >= 10^(-8) && cnt <= 20

x1 = x - P6(x)/dP6(x);

err = abs(x-x1);

x = x1;

end

sol(i) = x;

end

a = num2cell(sol);

b = sprintf('%1.8f ',a{:});

disp('zeroes of P6(x) are at x = ')

disp(b)

Explanation / Answer

clc

clear

P0 = (x) 1;

P1 = (x) x;


for i = 0:4;

P = (x) ((2*i+3)/(i+2)).*x.*P1(x) - ((i+1)/(i+2))*P0(x);

P0 = P1;

P1 = P;

end

P6 = P;

dx = 0.0001;

dP6 = (x) (P6(x+dx)-P6(x))/dx; % differentiation of P6(x)


x0 = [-0.9,-0.7,-0.3,0.3,0.7,0.9]; %six initial guess because there are 6 solutions

sol = zeros(size(x0));

for i = 1:length(x0);

x = x0(i);

cnt = 1;

err = 1;

while err >= 10^(-8) && cnt <= 20

x1 = x - P6(x)/dP6(x);

err = abs(x-x1);

x = x1;

end

sol(i) = x;

end

a = num2cell(sol);

b = sprintf('%1.8f ',a{:});

disp('zeroes of P6(x) are at x = ')

disp(b)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote