Problem with function handle on Root Bracketing

2 visualizaciones (últimos 30 días)
Eligijus Rupsys
Eligijus Rupsys el 16 de Mzo. de 2020
Comentada: Eligijus Rupsys el 17 de Mzo. de 2020
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 Rupsys
Eligijus Rupsys el 17 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 !
Eligijus Rupsys
Eligijus Rupsys el 17 de Mzo. de 2020
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 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.
Eligijus Rupsys
Eligijus Rupsys el 17 de Mzo. de 2020
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)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by