Question: clear all; close all; % Start fresh % Organizing Inputs % f = @ ( x ) sin ( 3 * pi * cos (

clear all; close all; % Start fresh
%Organizing Inputs%
f = @(x) sin(3*pi*cos(2*pi*x).*sin(pi*x));
a =-3; b =5; n =4^9;
x0= linspace(a,b,n); % Vector containing initial starting points
q = zeros(size(x0)); % Preallocate a vector for storing roots.
%%%%%%%%%%%%%%%%%%
tic
for i=1:n
q(i)= fzero(f,x0(i));
end
t1=toc
%Processing Outputs%
q = unique(q); % keep roots with unique values only.
showsavePlot = true;
switch showsavePlot
case true
%Plot the function and roots if possible
xx = linspace(a,b,1001);
fig = figure('Position',[1001001200300]);
plot(xx,f(xx),'-k','linewidth',2);
hold on
plot(q,f(q),'o','markerfacecolor','r')
xlim([a,b]); ylim([-1,1]);
yticks([-101])
xlabel('x'); ylabel('f(x)');
pbaspect([411])
print(fig,'MySavedPlot','-dpng')
end
% Create a parallel pool if none exists
if isempty(gcp())
parpool();
end
tic
parfor i=1:n
q(i)= fzero(f,x0(i));
end
tp= toc
%Processing Outputs%
%q = unique(q); % keep roots with unique values only.
q= uniquetol(q,1e-10);
showsavePlot = true;
switch showsavePlot
case true
%Plot the function and roots if possible
xx = linspace(a,b,1001);
fig = figure('Position',[1001001200300]);
plot(xx,f(xx),'-k','linewidth',2);
hold on
plot(q,f(q),'o','markerfacecolor','r')
xlim([a,b]); ylim([-1,1]);
yticks([-101])
xlabel('x'); ylabel('f(x)');
pbaspect([411])
print(fig,'MySavedPlot','-dpng')
end
speedup=t1/tp
eff= speedup/n
Convert the above code of matlab to python and R

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!