I just started using MATLAB, and for that I am not familiar with MATLAB itself. My sample code is as follows:
function Problem1 = BisectionMethod1()
a = input('enter function:', 's');
f = inline(a);
iteration_counter = 0;
al = input('enter left bound: ');
ar = input('enter right bound: ');
break;
disp(f('al'))
disp(f('ar'))
When I set a as x+1, and set my left and right bound as 1 and 2, it displays f(al) and f(ar) correctly.
The problem seems to begin when I have a coefficient in front of 'x'.
For example, when I set a as 2x+1, and set my left and right bounds as 1 and 2, MATLAB would give me error.
Like I said, I am new to MATLAB, is there any way to solve this?
One point I need to make is that multiplication requires a *
operator. By doing 2x
, MATLAB would interpret this as a variable named 2x
and MATLAB does not support variables where there is a number that comes first. Therefore, you need to do 2*x + 1
. In addition, you need to remove your break
statement. Your code will exit prematurely if you leave this in.
Also, simply remove the single quotes when calling f
. You are inputting the variable, not the actual name of the variable itself. As such, you would do:
disp(f(al));
disp(f(ar));
Using your code, this is what I get:
>> a = input('enter function:', 's');
enter function:2*x + 1
>> f = inline(a);
>> al = input('enter left bound:');
enter left bound:1
>> ar = input('enter right bound:');
enter right bound:2;
>> disp(f(al))
3
>> disp(f(ar))
5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.