Question

(b) Write a MATLAB script for computing the norm of a vector in a numerically stable fashion. Demonstrate the performance of

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


Matlab code for norm of a vector clear all close all %for any vector x-[1;2;3;4;5] ; nn-norm vec (x); fprintf\tFor vector x=\

%%Matlab code for norm of a vector
clear all
close all

%for any vector
x=[1;2;3;4;5];
nn=norm_vec(x);

fprintf('\tFor vector x=\n')
disp(x)

fprintf('\tNorm of x=%f\n',nn)

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%function for norm of a vector
function nn=norm_vec(x)
  
    ln=length(x);
    sum=0;
    for i=1:ln
        sum=sum+(x(i)).^2;
    end
  
    nn=sqrt(sum);
  
end

%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
(b) Write a MATLAB script for computing the norm of a vector in a numerically stable fashion. Demonstrate the perfor...
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
  • The Euclidean norm of an n-dimensional vector is defined by Implement a robust routine for computing...

    The Euclidean norm of an n-dimensional vector is defined by Implement a robust routine for computing this quantity for any given input vector I. Your routine should avoid overflow and harmful underflow. Compare both the accuracy and performance of your robust routine with a more straightforward naive implementation. Can you devise a vector that produces significantly different results from the two routines? How much performance does the robust routine sacrifice?

  • matlab Write a script using a loop that will take a vector (of any size) of...

    matlab Write a script using a loop that will take a vector (of any size) of numbers and remove all numbers that are between 5.0 and 9.0 and assign the remaining vector to a nevw variable. Example: A-[1,5,8,10,13,18.20,9,4,6,8] B-[1,10,13,18,20,4]

  • Write Matlab script for computing inverse of a matrix using LU decomposition/factorization. You are not allowed...

    Write Matlab script for computing inverse of a matrix using LU decomposition/factorization. You are not allowed to use the Matlab’s lu function.

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

  • *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop,...

    *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop, not the built-in sort function). 4)write a function that will receive a vector and will return two index vectors: one for ascending order and one for descending order. Check the function by writing a script that will call the function and then use the index vectors to print the original vector in ascending and descending order. **Please make sure they work. I have found...

  • Question 2 Problem Definition Write a MATLAB script that uses nested loops to create a 100 by 100...

    Question 2 Problem Definition Write a MATLAB script that uses nested loops to create a 100 by 100 element 2D array representing an image that shades from white at the top to black at the bottom. Your script should save the image in a file called test2.png. Testing The image file produced should be shaded from white at the top of the image to black at the bottom of the image like SO: Coding Write your code in a MATLAB...

  • Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates...

    Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates the determinant of a 3x3 matrix by using the formula: A22 A23 11 A32 A33 A23 A12 A31 A33 21 A For the function name and arguments use d3-det3by3 (A), where the input argument A is the matrix and the output argument d3 is the value of the determinant. Write the code of det3by3 such that it has a subfunction that calculates the 2x2...

  • MATLAB Question: Write a script for the problem pictured. Please send the script, not just the...

    MATLAB Question: Write a script for the problem pictured. Please send the script, not just the printout. Make sure your script allows you to input a value for n so when prompted, in the command window, user types n value. The n value should be the f_k if k ==1 .... Please also send print outs if you can. BELOW IS A SAMPLE ANSWER THAT DID NOT PUT THE INPUT SCRIPT NEEDED FOR THIS QUESTION TO BE CORRECT. YOU CAN...

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

  • 1. Use the Trapezoidal Rule to numerically integrate the following polynomial from a = 0.5 to...

    1. Use the Trapezoidal Rule to numerically integrate the following polynomial from a = 0.5 to b = 1.5 f(x) = 0.2 + 25x – 200x2 +675x3 – 900x4 + 400x5 Use three different number of segments and show that higher number of segments give lesser relative error considering the exact value of the integral, which is 49.3667. 2. Write a MATLAB or OCTAVE code to solve the above problem numerically and verify your result. Copy/paste the code and answers...

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