{"id":4374,"date":"2017-09-22T09:09:27","date_gmt":"2017-09-22T09:09:27","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4374"},"modified":"2024-09-04T07:05:06","modified_gmt":"2024-09-04T07:05:06","slug":"how-to-use-fzero-function-in-matlab-octave-script","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-use-fzero-function-in-matlab-octave-script\/","title":{"rendered":"How to use fzero function in Matlab\/Octave?"},"content":{"rendered":"<h2>How to use fzero function in Matlab\/Octave?<\/h2>\n<p>Let&#8217;s discuss the usage of the Matlab function <strong>fzero<\/strong> function and how to use it in scripts. We can use the function to find the roots of the problem or values of the variable where the function tends to zero.<\/p>\n<p>Consider the below function:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/latex.codecogs.com\/gif.latex?\\LARGE&amp;space;y&amp;space;=&amp;space;f(x)\" alt=\"\\LARGE y = f(x)\" align=\"absmiddle\" title=\"\"><\/p>\n<p>The problem of root finding is such that :<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/latex.codecogs.com\/gif.latex?\\LARGE&amp;space;f(xRoot)&amp;space;=&amp;space;0\" alt=\"\\LARGE f(xRoot) = 0\" align=\"absmiddle\" title=\"\"><\/p>\n<p><strong>xRoot<\/strong> is the root of the function.\u00a0 In some cases we may want to approximate and find an approximate root value under given tolerance <strong>tol <\/strong>value.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/latex.codecogs.com\/gif.latex?\\LARGE&amp;space;f(xRoot)&amp;space;&lt;&amp;space;tol\" alt=\"\\LARGE f(xRoot) &lt; tol\" align=\"absmiddle\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3>Problem<\/h3>\n<p>Let&#8217;s consider a small problem and develop a script to find the solution<strong>.( Based on Angry Birds Game)<\/strong><\/p>\n<p>Assume a Pig is at the coordinates <strong>( 100,25)<\/strong>. We need to shoot an angry bird to hit the pig. The initial velocity of the bird is <strong>60 m\/s<\/strong>. The gravity constant is <strong>9.81.<\/strong> y can be calculated as per the formula:<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/latex.codecogs.com\/gif.latex?\\huge&amp;space;y&amp;space;=&amp;space;xtan\\theta&amp;space;-&amp;space;\\frac{1x^{2}g}{2&amp;space;v^{2}cos\\theta&amp;space;^{2}}\" alt=\"\\huge y = xtan\\theta - \\frac{1x^{2}g}{2 v^{2}cos\\theta ^{2}}\" align=\"absmiddle\" title=\"\"> ;<\/p>\n<h3><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4378\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Angry-Birds-vs-Pig.png\" alt=\"Angry Birds vs Pig\" width=\"1701\" height=\"902\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Angry-Birds-vs-Pig.png 1701w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Angry-Birds-vs-Pig-300x159.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Angry-Birds-vs-Pig-1024x543.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Angry-Birds-vs-Pig-768x407.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Angry-Birds-vs-Pig-1536x815.png 1536w\" sizes=\"auto, (max-width: 1701px) 100vw, 1701px\" \/><\/p>\n<h3><\/h3>\n<h3>fzero function<\/h3>\n<p>root = fzero(@(theta0) f(theta0), [45 90]);<\/p>\n<p>To know more information on fzero, browse the help documentation. To get the help, type the following in the command window prompt.<\/p>\n<p><strong>&gt;&gt; help fzero<\/strong><\/p>\n<p>In the script we have <strong>two initial guesses<\/strong> within two intervals. 0 -45 degrees and another in the interval 45 &#8211; 90 degrees.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-19965\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/fzero-help.png\" alt=\"fzero help\" width=\"1681\" height=\"795\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/fzero-help.png 1681w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/fzero-help-300x142.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/fzero-help-1024x484.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/fzero-help-768x363.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/fzero-help-1536x726.png 1536w\" sizes=\"auto, (max-width: 1681px) 100vw, 1681px\" \/><\/p>\n<h3>MATLAB\/Octave Script<\/h3>\n<pre>% Projectile of Angry Bird\r\n        %Intial Velocity\r\n        v0=60;\r\n        %Gravity constant\r\n        g=9.81;\r\n        %Pig location\r\n        xPig=100;\r\n        %Pig y location\r\n        yPig=25;\r\n\r\n        f = @(theta) xPig*tan(theta*pi\/ 180) - \r\n(0.5*xPig^2*g)\/(v0^2*cos(theta*pi\/180)^2)- yPig\r\n\r\n        thetaRoot1 = fzero(@(theta0) f(theta0), [0 45]);\r\n        fprintf('Theta Root 1:= %4.2f degrees \\n', thetaRoot1);\r\n        thetaRoot2 = fzero(@(theta0) f(theta0), [45 90]);\r\n        fprintf('Theta Root 2:= %4.2f degrees \\n', thetaRoot2);\r\n\r\n        xPoints1= linspace(0,100,100);\r\n        yPoints1= xPoints1.*tan(thetaRoot1.*pi\/ 180) - \r\n(0.5.*xPoints1.^2.*g)\/(v0.^2.*cos(thetaRoot1.*pi\/180)^2);\r\n\r\n        xPoints2= linspace(0,100,100);\r\n        yPoints2= xPoints2.*tan(thetaRoot2.*pi\/ 180) - \r\n(0.5.*xPoints2.^2.*g)\/(v0.^2.*cos(thetaRoot2.*pi\/180)^2);\r\n\r\n        figure\r\n        hold on\r\n        grid off\r\n        plot(xPoints1,yPoints1,':bo','LineWidth',7.0,...\r\n        'MarkerEdgeColor','g',...\r\n        'MarkerFaceColor','r',...\r\n        'MarkerSize',30)\r\n\r\n        plot(xPoints2,yPoints2,':ro','LineWidth',7.0,...\r\n        'MarkerEdgeColor','b',...\r\n        'MarkerFaceColor','k',...\r\n        'MarkerSize',30)\r\n        xlabel('x')\r\n        ylabel('y')\r\n        title('Angry bird vs Pig')<\/pre>\n<h3><\/h3>\n<h3>Roots:<\/h3>\n<p>Run the script in the MATLAB or Octave command prompt window.<\/p>\n<p>&gt;&gt; AngryBird<\/p>\n<p>Theta Root 1:= 22.25 degrees<br \/>\nTheta Root 2:= 81.79 degrees<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-4384 size-full\" title=\"Equation Roots fzero function\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Equation-Roots.png\" alt=\"Equation Roots fzero function\" width=\"1920\" height=\"1056\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Equation-Roots.png 1920w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Equation-Roots-300x165.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Equation-Roots-1024x563.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Equation-Roots-768x422.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Equation-Roots-1536x845.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Screenshot<\/h3>\n<p>The script plots the trajectory of the angry bird with two blue and green markers as shown below:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4379\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Plot-Angry-Bird.png\" alt=\"Angry Birds vs Pig\" width=\"1920\" height=\"964\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Plot-Angry-Bird.png 1920w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Plot-Angry-Bird-300x151.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Plot-Angry-Bird-1024x514.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Plot-Angry-Bird-768x386.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Octave-Plot-Angry-Bird-1536x771.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Octave Tutorial on this website can be found at:<br \/>\n<a href=\"https:\/\/www.testingdocs.com\/octave-tutorial\/\"><strong>https:\/\/www.testingdocs.com\/octave-tutorial\/<\/strong><\/a><\/p>\n<p>More information on Octave can be found on the official website:<br \/>\n<strong><a href=\"https:\/\/www.gnu.org\/software\/octave\/index\" rel=\"noopener\">https:\/\/www.gnu.org\/software\/octave\/index<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use fzero function in Matlab\/Octave? Let&#8217;s discuss the usage of the Matlab function fzero function and how to use it in scripts. We can use the function to find the roots of the problem or values of the variable where the function tends to zero. Consider the below function: The problem of root [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-4374","post","type-post","status-publish","format-standard","hentry","category-octave","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=4374"}],"version-history":[{"count":22,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4374\/revisions"}],"predecessor-version":[{"id":24026,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4374\/revisions\/24026"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}