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.
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).
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 .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 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).
Write a MATLAB script file to numerically solve any first order initial value problem using Rulers...
Write a MATLAB script file to numerically solve any first order initial value problem using Rulers method. Once code is working use it to solve the mixing tank problem below. Use a step size of 1 minute, and simulate the solution until the tank contains no more salt. Plot both the Euler approximation and the exact solution on the same set of axes. A tank contains 100 gallons of fresh water. At t=0 minutes, a solution containing 1 lb/gal of...
Write a Matlab m-script to compute the backward difference approximation A = f(a)−f(a−h) h of the...
Write a Matlab m-script to compute the backward difference approximation A = f(a)−f(a−h) h of the derivative T = f0(a) for f(x) = sin(x) and a = π/3 using each value of h in the sequence 2−n (n = 1,2,3,···,52).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT