Question

In: Advanced Math

Write a Matlab script-file probl1.m to execute the requested commands (as much as possible) in the...

Write a Matlab script-file probl1.m to execute the requested commands (as much as possible) in the exercises below. Increase N a number of times according to N = 4, 8, 16, 32, 64, 128, . . . (1) Determine for each N the (exact) error. (2) Determine for N ≥ 16 also the convergence ratio q(h/2).

This script should be based on a function-file trap.m (trapezoidal integration) as follows:

function [totarea] = trap(N)

format long;

a = 0; b = 1/2; h = (b-a)/N;

x = a:h:b; totarea = 0;

for i = 1:N

xl = x(i);

xr = x(i+1);

fxl = myfunct(xl);

fxr = myfunct(xr);

locarea = (h/2)*(fxl+fxr);

totarea = totarea + locarea;

end

end

You can refer to the integral as myfunct(). The interval is [0,1/2].

Solutions

Expert Solution


%Matlab code for Trapizoidal method
clear all
close all

%Matlab code for Trapizoidal integral for varing N
n=[4 8 16 32 64 128 256];
%exact integral for given myfunct(x) is 0.75;
for i=1:length(n)
     N=n(i);
    [totarea] = trap(N);
    err(i)=abs(totarea-0.75);
    fprintf('For N=%d integral using Trapizoidal is %f with error %e\n',N,totarea,err(i))
    if i>=3
        fprintf('\tConvergence ratio =%f\n',err(i)/err(i-1))
    end
end
  

%function for integrand
function f=myfunct(x)
    f=3*x^2+5*x;
end

%function for Trapizoidal integral
function [totarea] = trap(N)

    format long;

    a = 0; b = 1/2; h = (b-a)/N;

    x = a:h:b; totarea = 0;

    for i = 1:N

        xl = x(i);

        xr = x(i+1);

        fxl = myfunct(xl);

        fxr = myfunct(xr);

        locarea = (h/2)*(fxl+fxr);

        totarea = totarea + locarea;

    end

end

    %%%%%%%%%%%%%% End of code %%%%%%%%%%%%%%%


Related Solutions

To find a positive root for , write a MATLAB script file that uses Bisection method....
To find a positive root for , write a MATLAB script file that uses Bisection method. Choose any initial value that is needed. Use absolute relative approximate error to be less than 0.01. Your code should report the number of iteration and the value of x.
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio...
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds), takes Fourier transform of the signal (probably FFT).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (curve fit), and x for f(x).
Write a MATLAB script file that will give the value of f(x) using method of least...
Write a MATLAB script file that will give the value of f(x) using method of least squares and asks the user to enter values of x, y, n (linear, quadratic, or cubic), and x for f(x).
Write a .m function file on MATLAB that reads the parameters stored in the file missile...
Write a .m function file on MATLAB that reads the parameters stored in the file missile data.txt into MATLAB. The function should have the following declaration: function [X0, Y0, Z0, m0, mf, Thmag0, theta, phi, Tburn] = read input( input filename, M id) where input filename is a string variable denoting the name of the file to be read and M_id is an integer which denotes the missile ID. The outputs are the initial position (X0, Y0, Z0), initial and...
Your First Bash Script Now that we know how to execute multiple commands and error check...
Your First Bash Script Now that we know how to execute multiple commands and error check using the command-line, we will start to put logic into a script. Bash scripts are interpreted by bash just like commands on the command-line. Use an editor to make the following script, called count.sh: echo “1” echo “2” echo “3” You can now run this script using the command: $ bash count.sh This feeds the script to the bash interpreter, which interprets the commands....
a – DSB. Write the code for an m-file (script) to generate a DSB signal. The...
a – DSB. Write the code for an m-file (script) to generate a DSB signal. The modulating (message) signal is a single tone signal with frequency 1kHz and the carrier frequency is 30kHz. Time Vector: 3001 points over range from 0 to 3ms (3 cycles of the modulating signal). Plot your original message signal both in time and its spectrum. (Note: the Matlab examples 6.1 and 6.2 will help, but use the cosine functions for your signals instead of sine...
Write a program in a script file that creates m × n matrix with elements that...
Write a program in a script file that creates m × n matrix with elements that have the following values. The value of each element in the last row is the number of the column. The value of each element in the last column is the number of row +1. For the rest of the elements, each has a value equal to the sum of the element below it and the element to the right.
Write a MATLAB script file to integrate  using trapezoid method (do not use trapz command for this...
Write a MATLAB script file to integrate  using trapezoid method (do not use trapz command for this part, write your own script). Consider x=-0.5 to 3 with Δt=0.01, and compare your result with using “integral” and "trapz" commands (all in one file).
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT