Computational Methods MATLAB homework b. Write a MATLAB user-defined function th
ID: 2267898 • Letter: C
Question
Computational Methods MATLAB homework
b. Write a MATLAB user-defined function that solves a non-linear equation of the form (f(x) = 0) with the modified regula falsi method. The program should check if the endpoints stay the same; if an endpoint is unchanged in three iterations, the value of the function at that point should be divided by 2. If the same endpoint is unchanged for three more iterations, the value of the function at that point should be divided again by 2(the true value now is divided by 4), and so on. Name the function RegulaRootMod(Fun, a, b, ErrM ax), where the return value from the function is the solution. The input argument Fun is the name of the function that calculates f(x) for a given x, a and b are two points that bracket the root, and ErrMax the maximum estimated relative error Use the function RegulaRootMod in 2.b to solve the following: c. f(x) = 3/4-_ 2sin"() 3(-sin()cos() )Explanation / Answer
The Matlab function for the regula falsi method with the given modifications is given below:
function sol = RegulaRootMod(Fun,a,b,ErrMax)
% This function computes the solution using regula falsi method
iter = 0;%iteration count initialization
err = 1;%initial error
preva = a-1;%some value not equal to a
prevb = b-1;%some value not equal to b
counta = 0;countb=0;%Check for the same endpoints
disp('Iteration a b x Fun(a) Fun(b) Fun(c)');
disp('========= ======= ======= ======= ========== ========== ========');
while(abs(err) > ErrMax)%Check for stop
iter = iter + 1;
Fa = Fun(a);% Computing fun at a
Fb= Fun(b);% Computing fun at b
if(preva == a)%Check for same end point
counta = counta+1;
if(counta==3)%Check for same endpoint for 3 iterations
Fa = Fa/2;
counta= 0;
end
else
counta = 0;
end
if(prevb == b)
countb = countb+1;
if(countb==3)
Fb = Fb/2;
countb= 0;
end
else
countb = 0;
end
c = a - ((Fa*(b-a))/(Fb - Fa));%Update value
Fc = Fun(c);
if(Fc*Fa > 0)
b = c;
err = Fb;
sol = b;
else
a = c;
err = Fa;
sol = a;
end
preva = a;
prevb =b;
fprintf('%3d%17.4f%11.4f%11.4f%14.4f%14.4f%14.4f ', iter,a,b,c,Fun(a),Fun(b),Fun(c));
end
end
A Typical output when the following code is run:
clc;
close all;
clear all;
% Fun = @(x) (3/4)-((2*(sin(x))^3)/(3*(x-(sin(x)*cos(x)))));
a = -2;b=1;
Fun = @(x) 2*x^3 -4*x^2 +3*x-2;
sol = RegulaRootMod(Fun,a,b,0.0001);
display(['The root is ' num2str(sol)]);
Output:
Iteration a b x Fun(a) Fun(b) Fun(c)
========= ======= ======= ======= ========== ========== ========
1 -2.0000 1.0769 1.0769 -40.0000 -0.9103 -0.9103
2 -2.0000 1.1486 1.1486 -40.0000 -0.8007 -0.8007
3 -2.0000 1.2129 1.2129 -40.0000 -0.6772 -0.6772
4 -2.0000 1.2682 1.2682 -40.0000 -0.5493 -0.5493
5 -2.0000 1.3137 1.3137 -40.0000 -0.4277 -0.4277
6 -2.0000 1.3495 1.3495 -40.0000 -0.3207 -0.3207
7 -2.0000 1.3766 1.3766 -40.0000 -0.2329 -0.2329
8 -2.0000 1.3964 1.3964 -40.0000 -0.1648 -0.1648
9 -2.0000 1.4104 1.4104 -40.0000 -0.1144 -0.1144
10 -2.0000 1.4202 1.4202 -40.0000 -0.0782 -0.0782
11 -2.0000 1.4269 1.4269 -40.0000 -0.0529 -0.0529
12 -2.0000 1.4315 1.4315 -40.0000 -0.0356 -0.0356
13 -2.0000 1.4345 1.4345 -40.0000 -0.0238 -0.0238
14 -2.0000 1.4366 1.4366 -40.0000 -0.0159 -0.0159
15 -2.0000 1.4379 1.4379 -40.0000 -0.0106 -0.0106
16 -2.0000 1.4388 1.4388 -40.0000 -0.0070 -0.0070
17 -2.0000 1.4394 1.4394 -40.0000 -0.0047 -0.0047
18 -2.0000 1.4398 1.4398 -40.0000 -0.0031 -0.0031
19 -2.0000 1.4401 1.4401 -40.0000 -0.0020 -0.0020
20 -2.0000 1.4403 1.4403 -40.0000 -0.0014 -0.0014
21 -2.0000 1.4404 1.4404 -40.0000 -0.0009 -0.0009
22 -2.0000 1.4405 1.4405 -40.0000 -0.0006 -0.0006
23 -2.0000 1.4405 1.4405 -40.0000 -0.0004 -0.0004
24 -2.0000 1.4406 1.4406 -40.0000 -0.0003 -0.0003
25 -2.0000 1.4406 1.4406 -40.0000 -0.0002 -0.0002
26 -2.0000 1.4406 1.4406 -40.0000 -0.0001 -0.0001
27 -2.0000 1.4406 1.4406 -40.0000 -0.0001 -0.0001
28 -2.0000 1.4406 1.4406 -40.0000 -0.0001 -0.0001
The root is 1.4406
b) The given function can not be solved using regula falsi method.
Code for finding solution for the given function:
clc;
close all;
clear all;
Fun = @(x) (3/4)-((2*(sin(x))^3)/(3*(x-(sin(x)*cos(x)))));
a = -2;b=1;
sol = RegulaRootMod(Fun,a,b,0.0001);
display(['The root is ' num2str(sol)]);
Output:
Iteration a b x Fun(a) Fun(b) Fun(c)
========= ======= ======= ======= ========== ========== ========
1 -2.0000 1.1254 1.1254 0.5393 0.0848 0.0848
2 -2.0000 1.7088 1.7088 0.5393 0.3989 0.3989
3 -2.0000 12.2496 12.2496 0.5393 0.7516 0.7516
4 -2.0000 -38.1879 -38.1879 0.5393 0.7482 0.7482
5 -2.0000 91.4122 91.4122 0.5393 0.7502 0.7502
6 -2.0000 -240.8089 -240.8089 0.5393 0.7481 0.7481
7 -2.0000 614.7667 614.7667 0.5393 0.7506 0.7506
8 -2.0000 -1575.5790 -1575.5790 0.5393 0.7504 0.7504
9 -2.0000 4016.6736 4016.6736 0.5393 0.7498 0.7498
10 -2.0000-10293.3590-10293.3590 0.5393 0.7499 0.7499
11 -2.0000 26340.9318 26340.9318 0.5393 0.7500 0.7500
12 -2.0000-67419.1611-67419.1611 0.5393 0.7500 0.7500
13 -2.0000172515.3185172515.3185 0.5393 0.7500 0.7500
14 -2.0000-441456.9837-441456.9837 0.5393 0.7500 0.7500
15 -2.00001129651.96351129651.9635 0.5393 0.7500 0.7500
16 -2.0000-2890703.6298-2890703.6298 0.5393 0.7500 0.7500
17 -2.00007397100.69127397100.6912 0.5393 0.7500 0.7500
18 -2.0000-18928692.7097-18928692.7097 0.5393 0.7500 0.7500
19 -2.000048437241.248448437241.2484 0.5393 0.7500 0.7500
20 -2.0000-123947644.4505-123947644.4505 0.5393 0.7500 0.7500
21 -2.0000317173664.7833317173664.7833 0.5393 0.7500 0.7500
22 -2.0000-811626029.0424-811626029.0424 0.5393 0.7500 0.7500
23 -2.00002076896276.82992076896276.8299 0.5393 0.7500 0.7500
24 -2.0000-5314637535.2279-5314637535.2279 0.5393 0.7500 0.7500
25 -2.000013599799102.354013599799102.3540 0.5393 0.7500 0.7500
26 -2.0000-34800968940.7236-34800968940.7236 0.5393 0.7500 0.7500
27 -2.000089053333038.924789053333038.9247 0.5393 0.7500 0.7500
28 -2.0000-227881474769.2447-227881474769.2447 0.5393 0.7500 0.7500
29 -2.0000583133328855.4460583133328855.4460 0.5393 0.7500 0.7500
30 -2.0000-1492198870368.9158-1492198870368.9158 0.5393 0.7500 0.7500
31 -2.00003818436296701.24073818436296701.2407 0.5393 0.7500 0.7500
32 -2.0000-9771121022483.6660-9771121022483.6660 0.5393 0.7500 0.7500
33 -2.000025003639871748.363025003639871748.3630 0.5393 0.7500 0.7500
34 -2.0000-63982628543621.1020-63982628543621.1020 0.5393 0.7500 0.7500
35 -2.0000163727232368901.0900163727232368901.0900 0.5393 0.7500 0.7500
36 -2.0000-418966948206985.0600-418966948206985.0600 0.5393 0.7500 0.7500
37 -2.00001072108171317323.40001072108171317323.4000 0.5393 0.7500 0.7500
38 -2.0000-2743452522745398.5000-2743452522745398.5000 0.5393 0.7500 0.7500
39 -2.00007020310026468738.00007020310026468738.0000 0.5393 0.7500 0.7500
40 -2.0000-17964500008339080.0000-17964500008339080.0000 0.5393 0.7500 0.7500
41 -2.000045969944252155256.000045969944252155256.0000 0.5393 0.7500 0.7500
42 -2.0000-117633987785092990.0000-117633987785092990.0000 0.5393 0.7500 0.7500
43 -2.0000301017443186797820.0000301017443186797820.0000 0.5393 0.7500 0.7500
44 -2.0000-770283340799908480.0000-770283340799908480.0000 0.5393 0.7500 0.7500
45 -2.00001971103132205100000.00001971103132205100000.0000 0.5393 0.7500 0.7500
46 -2.0000-5043920012282858500.0000-5043920012282858500.0000 0.5393 0.7500 0.7500
Here the values are intentinally removed. However you will get this output if you run the program
754 -2.0000-40590916235671836000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000-40590916235671836000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000 0.5393 0.7500 0.7500
755 -2.0000103869417775700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000103869417775700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000 0.5393 0.7500 0.7500
756 -Inf103869417775700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000 -Inf NaN 0.7500 NaN
757 NaN103869417775700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000 NaN NaN 0.7500 NaN
The root is NaN
>>
The reason is that the first end point is struck at -2 and hence the width of the bracket never go down a particular value. So it never converge.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.