Welcome To Home of Information

Write a MATLAB function that will accept the values a, b and c as inputs and return the value of A as output.

Posted in
No comments
By Anonymous


The area, A, of a triangle with sides of length a, b and c is given by

A=sqrt (s(s -a)(s -b)(s-c))

where s =(a+b+c) / 2 .

Write a MATLAB function that will accept the values a, b and c as inputs and return the value of A as output.

a) You have to take input from the user for a,b and c.

b) The first line of the file must have the format:

function [list of outputs] = function name(list of inputs)

c) Document the function. That is, describe briefly the purpose of the function and how it can be used. These lines should be preceded by % which signify that they are comment lines that will be ignored when the function is evaluated.

d) What changes are required, if we want to return the value of ‘s’ too from this function.


 

Solution:
funtion Question7()
    SideA = input('Enter value for side A of a Triangle: ');        %getting input
    SideB = input('Enter Value for side B of a Triangle: ');           %getting input
    SideC = input(‘ Enter Value for side C of a Triangle: ');            %getting input
   
    Area = FindArea(SideA, sideB,SideC);   %findig Area
   
    FPRINTF('Area of Triangles with sides %.2f, %.2f, %.2f is %.2f \n', SideA, SideB, SideC, Area);
end
Function[Argument Area ] = FindArea( MA, MB, MC )
    S = (MA + MB + MC) / 2;    %Using formula
   
    Argument_Area =  sqrt(S * (S-MA) * (S-MB) * (S-MC));
end         


0 comments:

output of the following MATLAB code.

Posted in
No comments
By Anonymous




Q#6:
What will be the output of the following MATLAB code.
S = 1; n = 1;
while S+(n+1)^2 < 100
n = n+1; S = S + n^2;
end
[n,S]

Solution:

Here below is the output



%Output displayed

0 comments:

MATLAB program which will take input a charater ‘a’ value from user. You have to use switch statement to decide if value of a is ‘t’ then you have to call table function, if value of a is ‘f’ then call factorial function, if value of a is ‘p’ then call prime function, if value of a is ‘s’ then call search function.

Posted in
No comments
By Anonymous



Write a MATLAB program which will take input a charater ‘a’ value from user. You have to use switch statement to decide if value of a is ‘t’ then you have to call table function, if value of a is ‘f’ then call factorial function, if value of a is ‘p’ then call prime function, if value of a is ‘s’ then call search function.
You have to write four functions in your program;
Table(n1,n2)
Factorial(n3)
Prime(n4)
Search(char n5[], char c, char choice)
Table function will print the table of n1 from 1 to n2.
Factorial function will print the factorial of n3 if n3 is multiple of 2.
Prime function will tell that n4 is prime or composite.
Search function will take first argument n5 as an array of characters and second element a character to be search in the array and third element a character to decide which searching algorithm to be used.i.e. if user has passed the value of c as ‘s’ then Search function will perform the sequential search but if value of c is something else then Search function will perform binary search.
Structure of your program will be like this. You have to make it exactly working.
Switch(a)

{
case ‘f’:
factorial();
break;
case ‘p’:
prime();
break;
case ‘t’:
table();
break;
case ‘s’:
search();
break;
}


Solution:


function QUETION5()                                               %Start the function
    disp('[t] TABLE');                                                  %Display the Menu
    disp('[f] FACTORIAL');
    disp('[p] PRIME');
    disp('[s] SEARCH');
    c=input('ENTER CHOICE: ','s');                          %Choice input
   
    switch (c)                                                    %Switch cases check
        case 't'
            n1=input('ENTER NO: ');                             %input from user
            n2=input('ENTER LIMIT: ');
            TABLE(n1,n2);                                               %Call table function
        case 'f'
            n3=input('ENTER THE NO: ');                    %input from user
            FACTORIAL(n3);                                          %Call factorial function
        case 'p'
            n4=input('ENTER THE NO: ');                     %input from user
            PRIME(n4);                                                   %Call prime function
        case 's'
            n=input('ENTER NO OF ELEMENTS: ');                %input from user
            c=input('ENTER ELEMENT TO SEARCH: ');   %input from user
            for i=1:n
                value=input('ENTER VALUE: ');             %Input Array from user
                n5(i)=value;
            end
            disp('[s] SEQUENTIAL');                             %Choice to which algo apply
            disp('[b] BINARY');
            ch=input('ENTER CHOICE: ','s');
            SEARCH( n5, c , ch);
       
        otherwise
            disp('SELECT ANOTHER ONE');
    end
end
function TABLE(n1,n2)                                             %Table function
    for n=1:n2
        disp([num2str(n1),'*',num2str(n),'=',num2str(n1*n)]);
                                                    %Print table
    end
end
function FACTORIAL(n3)                                          %Factorial function
   if(mod(n3,2)==0)                                                   %Checking factorial condition
        fact=1;
        for i=n3:-1:1
            fact=fact*i;                                         %Calculate factorial
        end
        disp(fact);                                               %Printing factorial
   else
       disp('ERROR!!!');
   end
   
end

function PRIME(n4)                                                 %Prime function
if(mod(n4,n4)==0 && mod(n4,2)~=0)
%if(isprime(n4)==1)
    disp('YES PRIME');
else
    disp('NOT PRIME');
end
end
function SEARCH( n5, c , ch)                                   %Search function
count=0;
if(ch=='s')                                                                    %Giving Condition for sequential search
    for i=1:length(n5)                                                  %Using Sequential search algorithm
        if(c==n5(i))
        count=count+1;
        end
    end
end
    if(count==0)
        disp('NOT FOUND !!!');
    else
        disp('FOUND');
    end
   
    if(ch=='b')                                                              %Condition for Binary search is used here
        low=1;                                                                  %Use of Binary search Algorithm
high=length(n5);
index=0;
while low<=high && index==0
    mid=floor((low+high)/2);
    if n5(mid)==c
        index=mid;
        disp(['FOUND AT INDEX  :',num2str(index)]);
    elseif c<n5(mid)
        high=mid-1;
    else
        low=mid+1;
    end
end
    end
end


0 comments:

About the Author

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque volutpat volutpat nibh nec posuere. Donec auctor arcut pretium consequat.

Proudly Powered by Blogger.