Question

3. 100 Points Rewrite the Matlab user-defined function mybab(x), which was developed earlier in class and returns the square root of a number using the Babylonian method, in such a way that it will now include a check for the error in user input, or so called error-trap. This function will return two values, the square root and an integer error code. Identify all possible cases of user input error. You can use the following Matlab built-in functions a. b. ischar (x) which will return 1 if x is a character string, and zero otherwise isreal (n) which will return 1 if n is a real number, and zero otherwise Examples: yischar ( My Name) a3.5 % This will make y-: 1 This will make z 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The babylonian method uses the property of successive division to divide the number and then it is used to calculate the square root of the number. We can do the same in matlab using the inbuilt mean function.

The result will be stored in a variable result_sqrt and the while loop will run untill we get a solution which is very close to the actual solution.

function result_sqrt = mybab(x)
% dividing the number by 2
mid = x/2;
% getting one more intermediate value
mid2 = x/mid;
% we take the mean between the two values
result_sqrt = mean([mid mid2]);

% Repeat until we get a value which is close to the square root
while abs(x - result_sqrt^2) > 0.000000001
mid = result_sqrt;
mid2 = x/mid;
result_sqrt = mean([mid mid2])
end

Add a comment
Know the answer?
Add Answer to:
3. 100 Points Rewrite the Matlab user-defined function mybab(x), which was developed earlier in class and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Matlab Question. Please be detailed Write a user-defined function that performs LU decomposition (using Gauss Elimination...

    Matlab Question. Please be detailed Write a user-defined function that performs LU decomposition (using Gauss Elimination without partial pivoting) of a square matrix. Do not use built-in MATLAB functions lu( ), inv(), \, linsolve(). Matrices (in [A]*{x}={B} form) A=[15 -3 -1; -3 15 -6; -4 -1 12] B=[3800; 1200; 2350] Given code lines: function[L,U]=myLUFact_username(A) [m,n]=size(A); %numbers of rows/comlumns of A assert(m==n, 'A should be a square matrix');

  • (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so...

    (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so that it implements the composilu trapezidul rulo, using a soquence of w -1,2, 4, 8, 16,... trapezoids, to approximatel d . This M file uses the MATLAB built-in function trapz. If 401) and y=() f(!)..... fr. 1)). 3= ($1, then the execution of z = trapz(x, y) approximates using the composite trapezoidal rule (with trapezoids). (Note that the entries of are labeled starting at...

  • Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following...

    Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following math function with x the input argument and y the output y(x)=0.8x4-13x2-5x The function should work for x being a scalar or a vector. Write a script file named PBTask4pl.m to a) Use the function to calculate y(3) and y(5) and display the results in command window b) Use the function to make a plot of the function y(x) for -5:5: x 5:5. Label...

  • matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name...

    matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...

  • Create a MATLAB function to perform Lagrange Interpolation. Your function will be used as illustrated below:...

    Create a MATLAB function to perform Lagrange Interpolation. Your function will be used as illustrated below: >> ya = Lagrange(x, y, a) where "x" is a vector of ? independent data values, "y" is a vector of ? dependent function values corresponding to "x", "a" is an arbitrary value of "x" for which you want to know the Westimate of "y", and "ya" is the estimate of the function at x=a. Print an error message and exit the function if...

  • Write a Matlab function called sine_Taylor that inputs a positive integer n, a number x, and...

    Write a Matlab function called sine_Taylor that inputs a positive integer n, a number x, and outputs the nth Taylor polynomial of the sine function. Compute the absolute and error for n = 1: 10 at x = 1, obtained by comparing the output of your function to that of Matlab's built in sine function. Type on the Matlab console format long, then compute the previous error. Repeat the operation by typing format short. Comment on the result (quantify the...

  • a) Make a function which returns x/3 with a given integer x. double third(int x) {...

    a) Make a function which returns x/3 with a given integer x. double third(int x) { //Complete your code below. ______________________________________ return y; } b) Make a program which shows 2 ∗ n with a given user input n. #include<iostream> using namespace std; // Complete the blank. _______________________ int main(){ int n; cout<<"Enter:"; cin>>n; cout<<twice(n); return 0; } int twice(int x){ return 2*x; } c) Make a function which returns true if n is positive, otherwise returns false. ________ positive(int...

  • Write a user-defined MATLAB function for the following math function y(x). The input to the function...

    Write a user-defined MATLAB function for the following math function y(x). The input to the function is x and the output is y. y(x)=-(0.2*x^4)+((e^-0.5*x)*x^3)+7*x^2 Call the function to calculate y(x) over the interval -3 ≤ x ≤ 4 for each increment of 1. Display x and corresponding y(x) in a text file with labeled column headings. Use the type command to display the contents in the text file.

  • Please use MatLab and comment code. Problem 2 Write a MATI AB user-defined function y-M?SotxType): x...

    Please use MatLab and comment code. Problem 2 Write a MATI AB user-defined function y-M?SotxType): x is a vector containing numbers. and Type can be 'Ascending' or Descending'. This function should sort the given vector x in the ascending or descending order based on the user's request and return the desired vector as the output. Do not use MATLAB built-in function sort. Hint: When you call this function you need to use for Ascending or Descending since they are strings.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT