Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rewrite this code to be simpler in MATLABTask 4 . Fix what is wrong in the code. MATLAB. Functions: f ( x ) = 2

Rewrite this code to be simpler in MATLABTask 4. Fix what is wrong in the code. MATLAB. Functions:
f(x)=2*x4-15*x2+5*x +5 Interval: [0,2]
Tolerance: 10^(-12)Maximum number of iterations: 100
sims x
f(x)=%function;
x_0p =%beginning of the interval;
x_0k =%end of interval;
tol =10^-12;
max_iter =100;
iter =0;
conP =0;
conK =0;
%defining the function for convergence
%%g(x)= f(x)+x;
g(x)= x -(f(x)/diff(f(x)));
%print
string = x_0p:0.1:x_0k;
fplot(f(x),[x_0p x_0k],'-b')
hold on
lin = string;
plot(string,lin, '-r')
hold on
fplot(g(x),[x_0p x_0k],'-g')
hold on
plot(string,0,"Color","c")
axis([x_0p, x_0k,-2,2])
hold off
xlabel('x')
ylabel('f(x)')
%algorithm
fprintf('iter x1L f(x1L) x1D f(x1D)
')
fprintf('--------------------------------------------------------------------------
')
while iter <100
if abs(f(x_0p))< tol && konP ==0 && iter >1
conP =1;
interP = iter;
elseif conP ==0
x_1p=vpa((g(x_0p)),5);
x_0p = x_1p; %% setting a new value to check from the beginning of the interval
conP =0;
end
if abs(f(x_0k))< tol & konK ==0 & iter >1
conK =1;
interK = iter;
elseif conK ==0
x_1k=vpa((g(x_0k)),5);
x_0k = x_1k; %% setting a new value to check from the beginning of the interval
conK =0;
end
if konP ==1 & konK ==0 & iter ==99
fprintf('the function converges on the L side after %d iterations',interP)
fprintf('point obtained : %.15f',x_1p)
fprintf('function value at the obtained point : %.12f',f(x_1p))
break
end
if konK ==1 & konP ==0 & iter ==99
fprintf('function converges on D side after %d iterations',interK)
fprintf('point obtained : %.15f',x_1k)
fprintf('function value at the obtained point : %.12f',f(x_1k))
break
end
if konK ==1 & konP ==1
fprintf('function converges on both sides')
fprintf('from L side after %d iterations from D side after %d iterations', interP, interK)
fprintf('obtained points with L : %.15f, and with D: %.15f',x_1p, x_1k)
fprintf('function value in obtained points L : %.13f, D: %.13f ',f(x_1p), f(x_1k))
break
end
fprintf('%d %.3f %.3f %.3f %.3f
',iter,x_1p,f(x_1p),x_1k,f(x_1k))
iter = iter +1; %% iteration increment
end
if iter ==100
fprintf("convergence not reached in 100 iterations ")
fprintf("last calculated x0 are %.15f
and %.15f
",x_1p, x_1k)
fprintf("value of function f at these points is: %.15f
and %.15f
",f(x_1p), f(x_1k))
end
%% if the condition is not reached in 100 iterations, the program prints the required data

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

Describe ERP and how it can create efficiency within a business

Answered: 1 week ago

Question

Who holds the power in recruitment and selection?

Answered: 1 week ago

Question

Explain the effectiveness of various selection methods

Answered: 1 week ago

Question

Explain the nature of attraction in recruitment

Answered: 1 week ago