Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is MATLAB question Problem 3. Write a menu-driven program in the script Pro

ID: 3184069 • Letter: T

Question

This is MATLAB question

Problem 3. Write a menu-driven program in the script Prob3 to investigate the constant . Model it similar to the program in the book that explores the constant e. The constant is the ratio of a circle's circumference to its diameter. Many mathematicians have found ways to approximate . For example, Machin's formula is: -1 6*atan(1 /5)-4 * atan(1 /239) Leibniz found that can be approximated by: -4/ 1-4/3 +4/5-4/7+49-41 1 + … The menu-driven program should have the following options: Print the result from Machin's formula Print the approximation using Leibniz' formula, allowing the user to specify how many terms to use. Return an error message and ask for another input if the user inputs a negative number and/or a non-integer Print the approximation using Leibniz' formula, looping until a "good" approximation is found. Exit the program. You should also write your code such that if the user closes the menu box rather than pushing one of the buttons, an error message will be displayed asking the user to choose one of the options. The menu will then be displayed again. You may choose to write functions for this problem if you like. If you do make functions, you may make separate function files or include function code at the bottom of your Prob3 script. Just be sure that you are following all directions and the script runs correctly before you submit your files

Explanation / Answer

%%% Matlab Programm %%%

clc;
clear all;
close all;
format long
%%% restoredefaultpath
y=1;
while(y==1)
disp('Menu');
disp('Enter 1 : Machins method ' );
disp('Enter 2 : Leibniz method upto nth terms' );
disp('Enter 3 : Leibniz method upto Good Accuracy');
n=input('Enter the method = ');
if (n==1)
    val=16*atan(1/5)-4*atan(1/239);
    fprintf('Value of pi by Machins method : %f ',val);
    y=input('Enter 1 to continue and 0 to exit ');
    if (y==0)
        disp('You are existing the program');
        break;
    end
  
else if (n==3)
        an=input('Enter the number of bit accuracy you want ');
        acc=10^(-an);
        sum(1)=0;
        for n=1:10000
            sum(n+1)=sum(n)+4*(-1)^(n+1)/(2*n-1);
            err=abs(sum(n+1)-sum(n));
            if (err < acc)
                break;
            end
        end
         val=sum(end);
         fprintf('Value of pi by Leibniz method upto %d bit Accuracy: %f ',an,val);
    y=input('Enter 1 to continue and 0 to exit ');
    if (y==0)
        disp('You are existing the program');
        break;
    end
    else if (n==2)
            g=input('Enter the Number of terms you want to use ');
            val=0;
            for n=1:g
                val=val+4*(-1)^(n+1)/(2*n-1);
            end
            fprintf('Value of pi by Leibniz method using first %d terms = %f ',g,val);
        y=input('Enter 1 to continue and 0 to exit ');
    if (y==0)
        disp('You are existing the program');
        break;
    end
        else
        disp('Wrong choise ')
        y=input('Enter 1 to continue and 0 to exit ');
    if (y==0)
        disp('You are existing the program');
        break;
    end
        end
    end
end


end


OUTPUT:

Menu
Enter 1 : Machins method
Enter 2 : Leibniz method upto nth terms
Enter 3 : Leibniz method upto Good Accuracy
Enter the method = 1
Value of pi by Machins method : 3.141593
Enter 1 to continue and 0 to exit 1
Menu
Enter 1 : Machins method
Enter 2 : Leibniz method upto nth terms
Enter 3 : Leibniz method upto Good Accuracy
Enter the method = 2
Enter the Number of terms you want to use 35
Value of pi by Leibniz method using first 35 terms = 3.170158
Enter 1 to continue and 0 to exit 1
Menu
Enter 1 : Machins method
Enter 2 : Leibniz method upto nth terms
Enter 3 : Leibniz method upto Good Accuracy
Enter the method = 3
Enter the number of bit accuracy you want 5
Value of pi by Leibniz method upto 5 bit Accuracy: 3.141493
Enter 1 to continue and 0 to exit 0
You are existing the program
>>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote