Error using ==> sprintf Function is not defined for 'sym' inputs. How to solve this problem?
2 views (last 30 days)
Show older comments
I found the below program abt Bisection Method to Find roots of a Equation in net.....
format long %setting the number of digits to be displayed during the calculation
clc %clears the screen
syms x; %declaring x which wil be used as the independent parameter at the time to entry of function by the user
func = input('Please enter a expression for f(x):'); % prompting user to enter the function which will be stored in variable func
xl = input('Please enter the stating point of the interval :'); %prompting user to enter the starting point of the interval ,this will be stored in variable xl
xu = input('Please enter the end point of the interval :'); %prompting user to enter the end point of the interval ,this will be stored in variable xu
decimalplaces=input ('Please enter the number of decimal places :'); %prompting user to enter the tolerance level to the number of digits ,this will be stored in variable decimaldigits
if(decimalplaces<0) %checks for the wrong input of the user
nullkey=input('Invalid Input!\nPress any key to continue'); %Error Message
return %program terminates
end
function_value_at_xl=subs(func,x,xl) %stores function value at xl
function_value_at_xu=subs(func,x,xu) %stores function value at xu
check_limits=function_value_at_xl*function_value_at_xu; %product of function value at xl and xu
sprintf('f(%f)*f(%f) = %f',xl,xu,check_limits) %displays the product
if(check_limits>0) %checks if the root lies in the specified interval
sprintf('Root does not lie in the given interval\nThe program will now terminate') %error message
nullkey=input('Press any key to continue'); %error message
return %program terminates
end
disp('So the root lies in the given interval') %displays confirmation message
i=0; %counts the number of iterations
while(1) %infinite loop to do the required iterations
disp('-------------------------------------------------------------------------------------------------')
disp('Iteration =') %displays iteration number
disp(i) %displays iteration number
sprintf('\nThe Interval is [%f,%f]',xl,xu) %displays interval
xr=(xl+xu)/2 %finds midpoint
termination_check=abs(xl-xr); %calculates the variable to be used for termination condition
sprintf('Termination Condition:\n|xl-xr| = |%f - %f| = %f ',xl,xr,termination_check) %displays the termination condition
if (termination_check<.5*10^-decimalplaces) %if required accuracy is achieved loop beaks
sprintf('---------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------\nAccuracy upto %f decimal places achived \n The Root of the given problem is = %f',decimalplaces,xr) %displays the root
break
end
function_value_at_xr=subs(func,x,xr) %stores function value at xr
function_value_at_xl=subs(func,x,xl) %stores function value at xl
check=function_value_at_xr*function_value_at_xl; %variable to check in which sub-interval the root lies
sprintf('function value at xr * function value at xl=%f',check) %displays the value
if(check<0) %determines if the root is in lower sub interval
disp('Therefore root lies in lower sub-interval')
xu=xr %then upper limit of next inteval is the mid-point of the preceding interval
end
if (check>0) %determines if the root is in upper sub interval
disp('Therefore root lies in upper sub interval')
xl=xr %then lower limit of next inteval is the mid-point of the preceding interval
end
i=i+1; %increments the number of iterations
disp('So we will do further iterations')
end
k=-5:1:5; %x-axis variable to plot graph from -5 to 5
y=subs(func,x,k); %y-axis variable to plot graph found out by substituting k in the given function
plot(k,y) %plotting function
but i can't able to run it. when going to sprint function it displays the below fig on command window:
Please enter a expression for f(x):x^2-x-1
Please enter the stating point of the interval :1
Please enter the end point of the interval :2
Please enter the number of decimal places :2
function_value_at_xl =
-1
function_value_at_xu =
1
Error using sprintf
Function is not defined for 'sym' inputs.
Error in BisectionMethod_1 (line 17)
sprintf('f(%f)*f(%f) = %f',xl,xu,check_limits) %displays the product
What can i do regarding this problem? Any help or comments would be of great help! Thank you.
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!