Question

In: Mechanical Engineering

Use Matlab and write entier script Problem 1. Using the information above, find y(n) for all...

Use Matlab and write entier script

Problem 1.

Using the information above, find y(n) for all n between 1:N.

y(tn)=mx(tn)+b+r(tn)

y=mx+b

Plot the points on a graph, without connecting them with a line.   Use a command like:

plot( x, y, ‘.r’ )

Now we want to find the “best fit” solution.   As was mentioned before, this was defined in the prior exercise. (You may wish to read this to understand how the algorithm works.)   Use the MATLAB code below.

You have the vectors x and y already. You need a vector I, which is a column which each element equal to 1.

Programming Steps:

I = ones(length(x),1);

u = [ x , I ];

w = (u’ * u) \ ( u’ * y) ;

The result is an estimate of the values of m and b.   The estimate for m is w(1) and the estimate for b is w(2). The resulting estimate for y is:

                y_est = u * w;

Solutions

Expert Solution

MATLAB CODE:

clc;
clear;
n=10; %number of elements in x
m=5; %assumed m
b=1; %assumed b
r=(linspace(1,3,n))';
x=(1:1:n)'; %array x
y=m*x+b+r; %function for y
%best fitting

I = ones(length(x),1);
u = [x,I];
w = (u'*u)\(u'*y);
y_est = u*w;
%plotting
figure(1) % plot showing y v/s x
plot (x,y,'*','color','r');
xlabel('x ','FontSize', 16); ylabel('y(x) and y_estimate','FontSize', 16);
title('y v/s x, with best fit','FontSize', 20);
grid on
hold on
plot (x,y_est,'color','k');
hold off

SAMPLE OUTPUT:

PLEASE COMMENT IF ANY DOUBTS OR ERROR ARE THERE.. I WILL HELP YOU..


Related Solutions

Write script in Matlab to solve 1. Y=Ax where X is of dimension n by 1...
Write script in Matlab to solve 1. Y=Ax where X is of dimension n by 1 and A is of dimension m by n. 2. C= A*B where A is of dimension m by p and B is dimension of p by n. So write matlab script to obtain Y and C in above problems.
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer...
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as...
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as well as the tank dimensions in feet. Assume that the initial measurements are 7 meters in diameter and 11 meters tall. Display your final answers to the screen using disp and a statement without a semicolon, e.g. write the following into your script disp(‘The capacity in U.S. gallons is:’), capacity, where capacity is a variable that you defined in preceding calculations. Part B: In...
Use Matlab and write entier script Problem 2. Generate the estimate (linear estimate) for y_est, and...
Use Matlab and write entier script Problem 2. Generate the estimate (linear estimate) for y_est, and plot(x , y_est). Use “hold on” and plot(x,y) and plot(x,y_est), so that you can see the result of the line and how it fits the data.   Does it look like it did a pretty good job? An estimate of the “residual error” is the sum of the squares of the difference between y and y_est. This is often denoted as r2. r2 = sum(...
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 and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2 – K) S(n) – S(n-1) ;   Assume S(1) = 0, S(2) = 1; (a)    Case 1, Assume K = 1 (b)    Case 2, Assume K = 2 (c)     Case 3, Assume K = 4 Plot all of these on the same plot, for N = 1 to 20
write a MATLAB script that plots the two functions ( y=(2x^2)-(3x)-4 and y=4sin(2Pix) - 1 )...
write a MATLAB script that plots the two functions ( y=(2x^2)-(3x)-4 and y=4sin(2Pix) - 1 ) in the same figure for x in [-1,3]. Add a title, x-label, y-label, and legend. ( MATLAB code and graph with all descriptions.)
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it in the variable fMultiplier. The script should them prompt the user for a number, storing it in the variabl fIn. The script should then calculate the product of these two variables, saving the result to another variable fOut, and printing it. The script should then repeat the prompt for fIn, calculation, and output twice more, using the same variable fIn and fOut all three...
Using MATLAB or Octave, Write a script that prompts the user for a minimum and maximum...
Using MATLAB or Octave, Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer value: 20 A random...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT