False Position method GUI problems

I am doing right now a GUI for false positon method. Link here False Position Method
I've wrote the code:
syms x;
s = input ('Function ');
s = char (s);
f = inline (s);
s_2 = diff (s, 2, 'x');
s_2=char(s_2);
f_2 = inline (s_2);
n = input ('Iteration number ');
a=input('interval: a ');
b= input ('interval: b ');
i=0;
if (f(a)*f_2(a)<0)
x=a;
while (i<=n)
x=x-(f(x)/f(x)-f(b)*(x-b));
i=i+1;
end
elseif (f(a)*f_2(a)>0)
x=b;
while (i<=n)
x=x-(f(x)/(f(x)-f(a))*(x-a));
i=i+1;
end
end
fprintf('x_9 false method position %f',x);
Works fine.
Here is the GUI code that i can't make it work
function rez_Callback(hObject, eventdata, handles)
ca = get(handles.cap_a, 'String'); %endpoint "a" of the interval
a = str2num(ca);
cb = get(handles.cap_b, 'String'); %endpoint "b" of the interval
b = str2num(cb);
nrit = get(handles.nr_iter,'String'); %number of iterations
N = str2num(nrit);
function = get(handles.edit_fct, 'String');
f = inline(function);
i = 0;
if(f(a)*f_2(a)<0)
x = a;
while (i<=n)
x = x-(f(x)/f(x)-f(b)*(x-b));
i=i+1;
end
elseif (f(a)*f_2(a)>0)
x=b;
while (i<=n)
x=x-(f(x)/(f(x)-f(a))*(x-a));
i=i+1;
end
end
set(handles.edit_rez, 'String', x); %i display the result in an edit text
because i have no idee how to put this
s_2 = diff (s, 2, 'x');
s_2=char(s_2);
f_2 = inline (s_2)
I tried but i failed every time.
How GUI looks like
Test function x^3-6*x^2+10*x-4

1 Comment

Hello sorry but can you help me with code of bisection method I need it for my project 🙆🏻♀

Sign in to comment.

 Accepted Answer

Well i managed to solve it.
function rez_Callback(hObject, eventdata, handles)
% hObject handle to rez (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ca = get(handles.cap_a, 'String');
a = str2num(ca);
cb = get(handles.cap_b, 'String');
b = str2num(cb);
nrit = get(handles.nr_iter,'String');
N = str2num(nrit);
functie = get(handles.edit_fct, 'String');
f = inline(function);
s_2 = diff (function, 2, 'x');
s_2=char(s_2);
f_2 = inline (s_2);
i = 0;
if(f(a)*f_2(a)<0)
x = a;
while (i<=nrit)
x = x-(f(x)/f(x)-f(b)*(x-b));
i=i+1;
end
elseif (f(a)*f_2(a)>0)
x=b;
while (i<=nrit)
x=x-(f(x)/(f(x)-f(a))*(x-a));
i=i+1;
end
end
set(handles.edit_rez, 'String', x);

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Asked:

N/A
on 6 Jun 2015

Commented:

on 19 Dec 2020

Community Treasure Hunt

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

Start Hunting!