Question


photo of the code please and an explnation if possible,
Construct a simple MATLAB function program my_factorial which calculates product of first n positive integer numbers (i.e. fi
0 0
Add a comment Improve this question Transcribed image text
Answer #1

%2

function f = my_factorial(n)
if isnumeric(n) && length(n)==1 && n>=0 && (n==floor(n))%if n is a positive scalar integer
f=1;
for i=1:n
f=f*i;
end
else%else, we throw an errror
error('n must be a positive integer!!!')
end

con IIIIIIII function f = my_factorial (n) if isnumeric (n) && length (n)==1 && n>=0 && (n==floor (n)) if n is a positive sca

%3

function sx = my_sin(x,n)
if ~(isscalar(x) && isnumeric(x))%checking if x is a scalar value
error('x must be a scalar numeric value!!!')
elseif ~(isscalar(n) && isnumeric(n) && n>0 && floor(n)==n)%checking n
error('n must be a positive integer!!!')
else
sx=0;
for k=0:n-1
sx=sx+(-1)^k*x^(2*k+1)/(my_factorial(2*k+1));
end
%sx=sx+x;
end

N TILL III | 009 function sx = my_sin(x, n) if (isscalar (x) && isnumeric(x)) $checking if x is a scalar value error(x must

%4

clear
clc
x=pi/4;%checking at x = pi/4;
n=1:6;%value of n
er=zeros(1,length(n));
for i=1:length(n)
sinx=my_sin(x,n(i));
er(i)=abs(sinx-sin(x));%storing absolute error
end
semilogy(n,er,'-*')
xlabel('number of terms')
ylabel(' log of error')
grid on
title('my\_sin vs matlab\_sin')

IIIIIIIIIIIIII clear clc x-pi/4; $checking at x = pi/4; n=1:6; $value of n er-zeros (1, length (n)); for i=1:length (n) sinx=

my_sin vs matlab_sin log of error 10-12 1 1.5 2 2.5 4.5 5 5.5 6 3 3.5 4 number of terms

Add a comment
Know the answer?
Add Answer to:
photo of the code please and an explnation if possible, Construct a simple MATLAB function program...
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
  • This is the given code: /** * This program uses a Taylor Series to compute a...

    This is the given code: /** * This program uses a Taylor Series to compute a value * of sine. * */ #include<stdlib.h> #include<stdio.h> #include<math.h> /** * A function to compute the factorial function, n!. */ long factorial(int n) { long result = 1, i; for(i=2; i<=n; i++) { result *= i; } return result; } int main(int argc, char **argv) { if(argc != 3) { fprintf(stderr, "Usage: %s x n ", argv[0]); exit(1); } double x = atof(argv[1]); int...

  • MATLAB ONLY PLEASE...ALSO AS SIMPLE AS POSSIBLE...NEW TO LANGUAGE Write a code that will ask the...

    MATLAB ONLY PLEASE...ALSO AS SIMPLE AS POSSIBLE...NEW TO LANGUAGE Write a code that will ask the user to input a positive number x. If the number is negative the program will keep asking again and again (as long as the value is negative) until the user inputs a positive value. At the end of the program, calculate the following: y = e-x

  • Please answer the question and include matlab code and plots. Thank You Problem 1: The operation...

    Please answer the question and include matlab code and plots. Thank You Problem 1: The operation of signal dilation or decimation or down-sampling is defined by y(n) = x(nM) in which the sequence x(n) is down-sampled by an integer factor M. Explore Matlab’s downsample command to implement the above equation. Now generate x(n) = sin(0.125πn), for −50 ≤ n ≤ 50. Decimate x(n) by a factor of 4 to generate y(n). Plot both x(n) and y(n) using subplot and comment...

  • 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...

  • Write a MATLAB script, which asks the user to input the angle xd (in deg) and...

    Write a MATLAB script, which asks the user to input the angle xd (in deg) and then approximates sine of the angle using the first 5 terms of Taylor series as follows: n+12n-1 sin n-1 (2n-1)! 1 Note that x in Taylor's equation is in radian, so make sure to do the conversion first Start with defining the vector n=1:5 and then use element-wise operation and basic MATLAB commands to calculate the summation. To calculate the factorial of any number,...

  • use MATLAB and PLZ comment the code so that it is easy to understand Implement the...

    use MATLAB and PLZ comment the code so that it is easy to understand Implement the following series of sinusoidal signals in MATLAB: 1 sin(kt) where N 30 k=1 x(t) k 1 Write a MATLAB script which does the following steps First define and plot signal sin(t) for 0 St s 3Twhere T is the fundamental period of this signal The signal you just implemented is x(t) with N 1 a. Then modify your code to be able to define...

  • please show MATLAB code with results please MATLAB Provide printouts of the program and results where...

    please show MATLAB code with results please MATLAB Provide printouts of the program and results where you calculated the Fourier transform of the following: Each student will substitute the last two numbers from their student ID number for the letter "a" ult-a) Wribc an ID nunber 6(t+a) rect(t/a) (a/n)(sinc(at/n) expljat) cos(at) sin(at)u(t) See the attached examples Fourier Page 1 of Fourier Transform syms t w f-cos (2 *pi*t) ; fourier (f,w) ADe 2 Fourier Page 1 of Foutier Transform syms...

  • Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func:...

    Use Matlab code Consider the following function sin(x) Using the following parameters in your functions: -func: the function/equation that you are required to integrate -a, b: the integration limits n: the number of points to be used for the integration I:Integral estimate a) Write a function capable of performing numerical integration of h(x) using the composite trapezoidal rule. Use your function to integration the equation with 9 points. Write a function capable of performing numerical integration of h(x) using the...

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • Please code in MatLab or Octave Output should match Sample Output in the second picture Thank...

    Please code in MatLab or Octave Output should match Sample Output in the second picture Thank You 4. In this problem we will investigate using loops to calculate the product of two matrices. Given an mxn matrix A and an n x p matrix B, the matrix product C = AB is the m xp matrix with Cij = R=1 Qikbky. Write a program which calls a function get matrix.dimensions which should • print a description of the purpose of...

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