I need to find the roots of a function using secant or babylonian method.
조회 수: 2 (최근 30일)
이전 댓글 표시
I need to find the roots of the attached equation. Ultimately, I am trying to solve for DL. Pb, betaLR, and bR are input from the user, and I have written that code here.
prompt1 = 'Enter value for BLR: ';
BLR=input(prompt1);
prompt2= 'Enter value for bR: ';
bR=input(prompt2);
prompt3 = 'Enter value for desired breakage probability: ';
Pb=input(prompt3);
The next step, I thought, was to write a function to output alphaLR. I wrote it like this as a f1.m
function aLR = f(bR,BLR);
aLR = exp(bR/BLR)*-1;
The output of this function only gives me 'ans' and not an assigned variable 'aLR' for me to use from there on out, so that is my first issue.
My second issue is that I don't know whether to use secant or babylonian method. I am very new to MATLAB and I do not know which one would require fewer iterations, even though I have tried my best to learn what I could about each method. Furthermore, the program requires that the user input a convergence tolerance, and I do not understand how that will affect the number of iterations performed in the program.
Basically, I have tried everything I know how to do and have read every relevant think I could find and I am still completely stuck. I need help!
댓글 수: 0
답변 (1개)
Walter Roberson
2016년 10월 9일
The meaning of
function aLR = f(bR,BLR);
aLR = exp(bR/BLR)*-1;
is that there is one positional output value and two positional input values for a function named f, and that within the function the first positional input value will be called bR and the second positional input value will be called BLR and the one positional output value will be called aLR, and that the action of assigning to the variable aLR will change what is output to the first position. However, this does not in itself affect any variable outside of the function: variables outside the function are only affected if you assign the result of the call to f to the variable.
Consider
function customer = add_donut(customer)
customer = customer + donut;
If you invoke this with
add_donut(kelsey)
then this does not mean that afterwards there is a variable named "customer" containing kelsey-with-donut .
Indeed, when run in that form, it doesn't even mean that afterwards that kelsey would have gained a donut, because what would be passed in and operated on would be a copy of kelsey, a hypothetical kelsey, and the output would be what the hypothetical kelsey-with-donut would be like. It is not until you say "Okay, make the actual kelsey the same as this hypothetical version" that kelsey gets changed:
kelsey = add_donut(kelsey)
댓글 수: 2
Walter Roberson
2016년 10월 9일
Your earlier f had 2 input arguments and your new f has 3 input arguments. You might have forgotten to change one of the places that you called upon f.
Alternately if you are trying to pass the function f to another function, then you might have used something like
plot_response(f, x)
but that would mean that f is to be invoked with no arguments and the result of f is to be passed into plot_response . If you are trying to pass a function into a routine to be called by the routine then you need to pass its handle:
plot_response(@f, x)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!