Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

SOLVING QUADRATIC EQUATION BY GRAPHICAL METHOD

                                                  Here is a simple example to solve quadratic equation. This is just the tenth grade mathematics. I wrote the code to solve it using MATLAB. Here I mainly focused on the GUI and the graph related to the equation.
Additional Information: To compute complex calculation use Texas Instruments.

function quad_eqn()
%Solving the quadratic equation by graphical method
%Quadratic equation is split into two representing two equations
%parabola and a straight line.
%The x-co-ordinates of the points of intersection of the parabola and the
%straight line will give the roots of the given quadratic equation.





clear all;
%global to the functions inside parabola()
global e1 e2 e3 g1 g2 g3 sol;




figure('units','pixels','position',[200 300 500 300],'menubar','none','numbertitle','off','resize','off','color','w');
title('SOLVING THE QUADRATIC EQUATION','fontsize',20,'fontname','Time New Roman','color','k');
axis off;
uicontrol('style','push','units','pixel','position',[10,200,220,30],'string','ENTER THE GRAPH EQUATION:');
uicontrol('style','push','units','pixel','position',[10,130,220,30],'string','ENTER THE EQUATION OF STRAIGHT LINE:');
sol=uicontrol('style','push','units','pixel','position',[10,60,220,30],'string','SOLUTION SET:');
set(sol,'Visible','off');




%Equation of a graph
uicontrol('style','edit','units','pixel','position',[240,200,40,30],'fontsize',14,'callback',{@graphx2});
uicontrol('style','push','units','pixel','position',[290,200,40,30],'string','x^2');
uicontrol('style','edit','units','pixel','position',[340,200,40,30],'fontsize',14,'callback',{@graphx});
uicontrol('style','push','units','pixel','position',[390,200,40,30],'string','x');
uicontrol('style','edit','units','pixel','position',[440,200,40,30],'fontsize',14,'callback',{@graphc});




%Equation to solve:
%Provide the input in each text box.
%If the co-efficient of x not present then give the value as 0.

uicontrol('style','edit','units','pixel','position',[240,130,40,30],'fontsize',14,'callback',{@equx2});
uicontrol('style','push','units','pixel','position',[290,130,40,30],'string','x^2');
uicontrol('style','edit','units','pixel','position',[340,130,40,30],'fontsize',14,'callback',{@equx});
uicontrol('style','push','units','pixel','position',[390,130,40,30],'string','x');
uicontrol('style','edit','units','pixel','position',[440,130,40,30],'fontsize',14,'callback',{@equc});




    function graphx2(source,event)
        g1=get(source,'string');
        
    end




 function graphx(source,event)
        g2=get(source,'string');
        
 end




 function graphc(source,event)
        g3=get(source,'string');
        
 end




 function equx2(source,event)
        e1=get(source,'string');
        
 end




 function equx(source,event)
        e2=get(source,'string');
        
 end


 function equc(source,event)
        e3=get(source,'string');
        
        solve();
    end


    function solve()
        %Initialize the variables
        inc=0;
        %preallocate the memory
        p=zeros(1,4);
        r=zeros(1,4);
      
    try
        %Convert the string into number  
      gr1=str2double(g1);
      gr2=str2double(g2);
      gr3=str2double(g3);
      eq1=str2double(e1);
      eq2=str2double(e2);
      eq3=str2double(e3);
    










  
      %Solving the two equations
        a=gr1-eq1;
        b=gr2-eq2;
        c=gr3-eq3;
        
    x=-10:10;   
    temp=power(x,2);
    y=(gr1*temp)+(gr2*x)+gr3;%ax^2+bx+c
     z=(a*temp)+(b*x)+c;
     
      catch
          display('ERROR OCCURRED');
          close all hidden;
          return
    end
     for i=1:21
        if(y(i)==z(i))
           inc=inc+1;
           p(inc)=y(i);
           r(inc)=x(i);
         end
     end
set(sol,'Visible','on');
if(inc==2)
     n1=num2str(r(1));
     n2=num2str(r(2));
     solset=strcat('{',n1,',',n2,'}');
     uicontrol('style','text','units','pixel','position',[230,60,200,30],'String',solset);
else
     uicontrol('style','text','units','pixel','position',[230,60,200,30],'String','none');
end
figure('units','pixels','position',[100 50 1000 650],'menubar','none','numbertitle','off','resize','off','color','w');


    plot(x,y);
    hold on
    plot(x,z);
    xlabel('X- axis');
ylabel('Y-axis');
title('GRAPH','fontsize',14,'color','k');
    if(inc>0)
      for i=1:inc;
        n1=num2str(r(i));
        n2=num2str(p(i));
        point1=strcat('(',n1,',',n2,')');
         text(r(i),p(i),point1);
      end
     end
    end


end


GUI

GRAPH

like button Like "IMAGE PROCESSING" page

MATLAB GUI & EXCEL

function gui_excel()
%MATLAB example to demonstrate the GUI and export the values to spreadsheethttp://www.assoc-amazon.com/e/ir?t=m089e-20&l=btl&camp=213689&creative=392969&o=1&a=1584883200.
%The sample registration form will get the data and store it in a excel sheet.
%NOTE: Do not open sample.xls while executing because data inconsistency may occur.
%While executing, a warning message will appear to notify the user that 
%addition of new spreadsheet by name SAMPLE FORM is created.




%Delete the file if exists already.

if('sample.xls')
    delete sample.xls;
end
%To clear all the values present in the global varibles.
clear all;
global file_handler  file_string  Nxt  dd  mm  year  age_today;
file_handler=2;
%Export the column names to the excel sheet.

A={'NAME','DATE OF BIRTH','AGE','ADDRESS','EMAIL ID','PHONE NUMBER'};
%Here sample.xls is created and it will be in the MATLAB current directory
xlswrite('sample.xls',A,'SAMPLE FORM','A1');%filename, matrix,sheetname and the range is specified
figure('units','pixels','position',[250 100 500 450],'menubar','none','numbertitle','off',...
            'name','GUI & EXCEL(2)','resize','off');
        title('SAMPLE REGISTRATION FORM','fontsize',20,'fontname','Time New Roman','color','r');
        axis off;
        Rform();%call the function Rform to fill the form
    function Rform(source,eventdata)
 uicontrol('style','push','units','pixels','position',[15 380 150 30],...
                 'fontsize',10,'string','ENTER YOUR NAME');
 uicontrol('style','push','units','pixels','position',[15 300 150 30],...
                 'fontsize',10,'string','DATE OF BIRTH');
  uicontrol('style','push','units','pixels','position',[15 250 150 30],...
                 'fontsize',10,'string','AGE');
  uicontrol('style','push','units','pixels','position',[15 200 150 30],...
                 'fontsize',10,'string','ADDRESS');
  uicontrol('style','push','units','pixels','position',[15 100 150 30],...
                 'fontsize',10,'string','EMAIL ID');
  uicontrol('style','push','units','pixels','position',[15 50 150 30],...
                 'fontsize',10,'string','PHONE NUMBER');
 
     
        file_string=num2str(file_handler);
     
   uicontrol('style','edit','units','pixels','position',[200 380 250 30],...
                 'fontsize',14,'callback',{@nam_call});
             uicontrol('style','text','position',[200 340 50 20],'fontsize',14,'string','DD');
 uicontrol('style','edit','units','pixels','position',[200 300 50 30],...
                 'fontsize',14,'callback',{@dob_call1});
             uicontrol('style','text','position',[280 340 50 20],'fontsize',14,'string','MM');
        uicontrol('style','edit','units','pixels','position',[280 300 50 30],...
                 'fontsize',14,'callback',{@dob_call2});
             uicontrol('style','text','position',[360 340 50 20],'fontsize',14,'string','YYYY');
             uicontrol('style','edit','units','pixels','position',[360 300 90 30],...
                 'fontsize',14,'callback',{@dob_call3});
 uicontrol('style','text','units','pixels','position',[200 250 250 30],...
                 'fontsize',14);
 uicontrol('style','edit','units','pixels','position',[200 150 250 80],...
                 'fontsize',14,'callback',{@add_call});
  uicontrol('style','edit','units','pixels','position',[200 100 250 30],...
                 'fontsize',14,'callback',{@email_call});
  uicontrol('style','edit','units','pixels','position',[200 50 250 30],...
                 'fontsize',14,'callback',{@phone_call});
   okButton=uicontrol('style','push','position',[200,10,30,30],'string','OK','callback',{@finish});
   Nxt=uicontrol('style','push','position',[400,10,50,30],'string','NEXT','callback',{@next_call});
   set(Nxt,'Visible','off');

   %writing to the excel sheet
    function phone_call(source,eventdata)
       pno=get(source,'String');
       pnum={pno};
       xlswrite('sample.xls',pnum,'SAMPLE FORM' ,strcat('F',file_string));
    end

function email_call(source,eventdata)
       mail=get(source,'String');
       e={mail};
       xlswrite('sample.xls',e,'SAMPLE FORM' ,strcat('E',file_string));
end
    function add_call(source,eventdata)
       address=get(source,'String');
       add1={address};
       xlswrite('sample.xls',add1,'SAMPLE FORM' ,strcat('D',file_string));
    end
    function  age_call(source,eventdata)
       age=get(source,'String');
    
    end
 
 
    function dob_call1(source,eventdata)
       dd=get(source,'String');
    end
    function dob_call2(source,eventdata)
       mm=get(source,'String');
    end

        function dob_call3(source,eventdata)
       year=get(source,'String');
       
           dob=strcat(dd,'-', mm,'-', year);
           b={dob};
     xlswrite('sample.xls',b,'SAMPLE FORM' ,strcat('B',file_string));
     age_calculator();
    end
     
 
 













 
    function nam_call(source,eventdata)
       names=get(source,'String');
       nam1={names};
     xlswrite('sample.xls',nam1,'SAMPLE FORM',strcat('A',file_string));
        end
 
 
    function finish(source,eventdata)
        set(Nxt,'Visible','on');
        set(okButton,'Visible','off');
    end
    end

%Pushbutton NEXT calls the Rform to fill another form
    function next_call(source,eventdata)
        set(Nxt,'Visible','off');
        file_handler=file_handler+1;%handle is incremented to point the next position in the excel sheet
        Rform();
    end

%calcute the age automatically
    function age_calculator()
        now=date;
        dv=datevec(now);% date vector returns in yyyy mm dd format
     
        dd1=str2num(dd);
        dd_diff=dv(3)-dd1;
     
       mm1=str2num(mm);
       mm_diff=dv(2)-mm1;
    
       years=str2num(year);
       year_diff=dv(1)-years;
    
       if(mm_diff <= 0)
           if(dd_diff <= 0)
               age_today=year_diff-1;
           end
       else
           age_today=year_diff;
       end
       uicontrol('style','text','units','pixels','position',[200 250 250 30],...
                 'fontsize',10,'string',age_today);
             age1={age_today};
       xlswrite('sample.xls',age1,'SAMPLE FORM' ,strcat('C',file_string));
    end
      end





Running the GUI: Fill the form and press ok. Click Next button to fill another form. The data will be stored in the excel sheet.
like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com