Problem with function handle on Root Bracketing

Hello everyone !
I'm trying to write a code for a graphical method of root bracketing, where the range of x values are incremented with a step size of 0.1. The script esentially has to take the two consequtive x values, compute the corresponding y values and multiply them to check if there is a change of sign, if there is, then it means that at that range of two x-values there is a root, and then it stores the x values.
I am struggling because i will later going to have to plug in this code into a bisection method code to find the actual root (that is however a problem for a later date). When i try to use 'function' and add the @(x) term (sorry i am not too confident with the terminology), i get the wrong answers. I need to figure out why im getting the wrong answers before i can move to the next stage of my script.
This is the faulty code;
function [x_1, x_2] = test_4(f, x_min, x_max)
x = x_min : 0.1 : x_max % the range of x-values with a step of 0.1
j = 0
for i = 1 : length(x)-1
if (f(i)*f(i+1)) < 0
j = j + 1
x_1(j) = (i*0.1) - 0.1;
x_2(j) = (i*0.1) + 0.1
end
end
% test_4 (@(x) sin(((2/9)+2)*(x-(2/4)))-3.*(((x-((2+5)/2)).*(x-((2-5)/2)))/((2/2)^2+(10-2/2)^2)), 0, 10)
end
And here is the code that i know works and gives me the desired & correct answers;
x_min = 0
x_max = 10
x = x_min : 0.1 : x_max
f = sin(((2/9)+2)*(x-(2/4)))-3.*(((x-((2+5)/2)).*(x-((2-5)/2)))/((2/2)^2+(10-2/2)^2))
j = 0
for i = 1 : length(x)-1
if (f(i)*f(i+1)) < 0
j = j + 1
x_1(j) = (i*0.1) - 0.1;
x_2(j) = (i*0.1) + 0.1
end
end
Looking forwards for your replies !
Thank you !

4 comentarios

Eligijus - when does your code become faulty? Is it the line that you have commented out? If so, then that is the line that you call from the command line like
>> [x_1, x_2] = test_4(@(x) sin(((2/9)+2)*(x-(2/4)))-3.*(((x-((2+5)/2)).*(x-((2-5)/2)))/((2/2)^2+(10-2/2)^2)), 0, 10)
You don't want to be calling test_4 from withinn test_4 (since the function, in this case, is not recursive).
darova
darova el 16 de Mzo. de 2020
Geoff Hayes, thank you for you response. My code becomes faulty when in the first script I shared, it gives me x values as 0.1, 0.2 and 0.3 and that's it. When i run the second script i get the correct 4 x-values for x_1 and x_2 (the 4 x-value ranges where there is a root in the f-expression) , and everything seems to fail once i add the @(x) term. I need to somehow translate the second script for it to work with any input for the f-expression and overall x-value range, in a sense that it would have the 'function [ ] = test [ ] ' code at the beggining of the script. I know for me to have that format in the script i must add the '@(x)' term for it to work but it doesnt, that is my main problem.
Your last comment, that my function is not recursive, I dont really understand what you mean by that. I'm sorry, in uni we didn't get a strong introduction to matlab, and I still tried to self-teach it by loaning books out but im still very shaky with the terminology.
Thank you !
Darova, thank you for your help, this does simplify my loop and makes it look much neater. I realised i used a very silly approach to extract the x values from my loop output, thank you !

Iniciar sesión para comentar.

 Respuesta aceptada

Steven Lord
Steven Lord el 16 de Mzo. de 2020
for i = 1 : length(x)-1
if (f(i)*f(i+1)) < 0
Do you want to evaluate your function with the loop index as input or do you want to evaluate your function with values taken from your x vector as input? What your code is doing is the former, but you probably want the latter.
x = 0:0.25:5;
for loopidx = 1:numel(x)
fprintf("Element %d of x is %f.\n", loopidx, x(loopidx))
end

5 comentarios

Steven, thank you for your response! Yes i want my loop to take the values from my x vector as input, and for a step size of 0.1, to calculate the y-values for the first interval of the two x-values, and multipy the corresponding y-values, and if there is a change of sign, i want to store the x-values, if there isn't a change of sign, then continue the loop. The stored x-values represent the interval with a width of 0.1 where there is a root in the expression. I will be using those stored x-values to itterate a root using the bisection method later on in my MATLAB project.
I do applogise, my matlab knowledge is shaky, and I don't understand what the script you shared is meant to do. I plugged it into my script and it does looks promising, however I am trying to figure it out how to use it to get the results that i want. Would you be able to help me out with that?
Thank you !
Instead of calling f with just i as input you want to call it with x(i) as input (and similarly for f(i+1).) Note the difference between the values shown for f(k) and f(x(k)) in the code below with a sample f that computes the square of its input.
x = [7 12 -5];
f = @(x) x.^2;
for k = 1:length(x)
fprintf('k is %d.\n', k)
fprintf('f(k) is %d.\n', f(k))
fprintf('x(k) is %d.\n', x(k))
fprintf('f(x(k)) is %d.\n', f(x(k)))
fprintf('\n')
end
Steven Lord, thank you so much ! Your example made me realise where i was wrong this whole time. Not only you helped me to solve my issue, but through your example i now understand how matlab works much more ! One last request if i may ask. All of the outputs of the loop are displayed in the command window, but i much prefer to see them on the workspace, how would i be able to do that? Here is what i have for the loop ;
for k = 1:length(x)-1
if (f(x(k))*(f(x(k+1)))) < 0
j = j + 1
x_1(j) = x(k)
x_2(j) = x(k+1)
end
end
Once again thank you and everyone who contributed to my question !
Steven Lord
Steven Lord el 17 de Mzo. de 2020
End the lines inside the if statement with a semicolon to suppress their output from being displayed in the Command Window. See the entry for semicolon in the table on this documentation page.
Since it seems like you're relatively new to MATLAB you might find the MATLAB Onramp course that's available from the Tutorials section on the Support section of the MathWorks website (click Support on the top of this page) useful. It's a free two hour course that teaches the essentials of how to work with MATLAB, and I think you might learn even more about how MATLAB works from it.
Thank you! My university unfortunately did not provide us a strong introduction to the features of matlab, while being more focused on the algorithms of numerical methods. I will definetily give the online course a go!
Thanks for all of your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Mzo. de 2020

Comentada:

el 17 de Mzo. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by