Matlab test 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Q:The minimum value in matrix A

value=min(A(:));

Q:Write code to open cameraman.tif and then produce and display an image that is alpha times brighter than original image. Give alpha a value.

Iin=imread('cameraman.tif'); alpha=4; Iout=alpha*Iin; image(Iout)

Q: write matlab code to open cameramn.tif and then produce and display an image that is a "contrasted adjusted" version of the orginal image with output from 0-255

Iin=imread('cameraman.tif'); lo=min(Iin(:)); hi=max(Iin(:)); Iout=imread('cameraman.tif'); for i=1:size(Iin,1) for j=1:size(Iin,2) Iout(i,j)=(Iin(i,j)-lo)*(255/(hi-lo) end end

Q:Get the number of rows of matrix A and set it equal to nc

nc=size(A,1);

Q: For a linear system of equations if we have more variables than equations then the system is considered to be ______________-determined which usually solved using _______________. If we have more equations than variables the system is considered to be _________-determined which is usually solved using __________________.

under-determined estimation over-determined least-squares

Q: Use the command fprintf to form the output below by completing the code with the proper parameters assume val-pi use variable in your answer

val = pi; fprintf('Pi is approximately: %.3f\n',val);

how to access a 5th element in cell array W

w{5}

Q:The gateway arch in St Louis ect write a matlabcommands to sufficiently plot the shape of the arch which can be described by equations y=-127.7cosh(x/127.7)+757.8 for -315<=x<=314

x = -315:.1:315; y = -127.7*cosh*(x/127.7) + 757.8; plot(x,y)

Q:Write code to create a plot 2 full cycles of sine in degrees with 200 linearly spaced elements. Create two vectors x and y and then use the plot command to show the plot Extract the length of the y vector and put it in a varible len.

x = linspace(0,720,200); y = sind(x); plot(x,y) len = length(y);

Q:Use Matlabs built in functions rand() and randn() to form 2 random varibles x and y where x is uniform on a and b U(a,b) and y is normal with mu and standard deviation assume the varibles a, b, mu, sigma already exist?

x = rand(1)*(b-a)+a; y = randn(1)*(sigma)+mu;

Write MatLab code to solve for the following simultaneous equations. You may use the inv( ) function and the built in matrix multiplication capabilities in MatLab. You must initialize the proper data variables and the output variables x,y,z,w.

A=[1 2 3 4; 2 2 -2 3; 0 1 1 0; 1 -1 1 -2]; y= [12;10;-1;-4]; ans =zeros(size(y)); Ainv = inv(A); ans = Ainv * y; x=ans(1); y=ans(2); z=ans(3); w=ans(4);

Q:Place element by element marking only where they go a and b scalers A and B matrix

A=b*A.*A./B+B/a;

Q:take the inverse of a square matrix A

Ainv=inv(A);

Q:Each element of D is the product of each element of A and B

D=(A.*B);

Q:Create an Identiy matrix of size 11x11 multiply it by a value sigma squared and give it a varible name P

P=(sigma^2)*eye(11);

Q: write a function to preform the following matrix multiplication use only "for-loops" and generic input variable size of the input variables

S=HAH^t-Q^tR+D function S= multiplication_routine(A,H,Q,R,D) if (size(H,2)~=size(A,1) || size(A,2)~=size(H,2)... || size(Q,1)~=size(R,1) || size(H,1)~=size(Q,2) ... || size(H,1)~=size(R,2) || size(H,1)~=size(D,1) ... || size(H,1)~=size(D,2)) return end S=zeros(size(H,1),size(D,2)); for i=1:size(H,1) for j=1:size(D,2) for k=1:size(A,1) for l=1:size(A,2) S(i,j)=S(i,j)+H(i,k) * A(k,l) * H(k,i); end end for p=1:size(R,1) S(i,j)=S(i,j)-Q(p,i)*R(p,j); end S(i,j)=S(i,j)+D(i,j); end end end

Q:Matlab stores matires ______________ major order where the __________ dimernsions varies the fasted in loops theb the code runs faster?

columns, 1st

4 major parts of a matlab class

constructor destructor properties methods

Q: Write a function to preform matrix multiplication, C=A'*B' use only for loops and generic input sizes do not preform unnecessary looping operations Preform error checking on the size of input variables

function C=matrix_transpose_multiply(A,B) if size(A,1)~=size(B,2) return end C=zeros(size(A,2),size(B,1); for i=1:size(A,2) for j=1:size(B,1) for k=1:size(A,1) C(i,j)=C(i,j)+A(k,i)*B(j,k); end end end end

Q: Write a function to calculate the mean and standard deviation of a one dimensional array of values, x. Use for loops and not matlabs built in function

function [mu, sigma]=calculate_stats(x) s1=0; for i=1:length(x); s1=s1+x(i); end mu=s1/length(x); s2=0; for j=1:length(x) s2=s2+((x(i)+mu)^2); end sigma=sqrt(s2/(length(x)-1)); end

Q:Write a function to find a vectors minimum value and its index in the vector using only "for" "if" statements and pass both values back out to a calling routine

function [xmin,imin] = find_min_val_and_index(x) xmin = x(1); imin = 1; for i = 2:length(x) if ( x(i) < xmin) xmin = x(i); imin = i; end end end

Q:Write a function to find the average value of a 2D matrix using "for" statements function avg=find_average_value(mat)

function avg = find_average_value(mat) S2 = 0; for i = 1:size(mat,1) for j = 1:size(mat,2) S1 = mat(i,j); S2 = S1 + S2; end end avg = S2/size(mat); end

Q: Write a function to form the dot-product of two vectors x and y use for loops, vectors, and generic input variable sizes. Preform error checking on the size of the input variables

function dp= dot_products(x,y) if (length(x)~=length (y)) return end for i=1:length(x) dp=dp+x(i)*y(i); end end

Q:Write a function called nat_log_2 to implement the following estimation equation N is the only input varible. (Sum formula on test)

function out = nat_log_2(N) sum = 1; for n =1:N; sum=sum+((-1)^(n+1)/n); end out = sum; end

Q:write a functions sinh(x) that estimates the value of the following hyperbolic sine infinte series with the finite series 1 to N x and N are the input (Product on test)

function out = sinh(x,N) prod = 1; for k = 1:N; prod = prod(1+(x^2/(pi^2 * k^2)); end out = x*prod; end

Write a Matlab to implement the following graph for a single value of x. Assume this function will be called in a loop, looping over al values of x (See test for graph)

function y = graph(x) if x<-3 y = x+5; elseif x < -2 y = 3; elseif x <= 0 y = 4; elseif x <= 2 y = 3 - x ^ 2; elseif x <=5 y = -1; else y = x-6; end end

Q:write two different logical expressions that will allow the forming of the inverse of a square matrix, A

if (Det(A)~=0), "code for inverse of A" end if (rank(A)==N), " code for inverse of A" end

Q: Display a large array A as an image

image(A)

Q:How does matlab structures store data

in a way that it 'HAS' the data variable

Q: How does Matlab cell-array store data

in a way that it 'IS' the data varible

3 steps major

input data process data output data

Define and Algorithm

is a set of steps needing to be accomplished to reach a goal

Draw a picture for the problem above

picture

interperlation

step 1 adjucacent values xr=floor(x) xr+1=ceil(x) step 2 determine A a is related to each type step 3 y=a*x(i+1)+b*x(i)

Q: Matlab built in timing function

tic time = toc;

Two main general forms of algorithm

top down-problem driven dividing into small mangible parts to reach solution bottom up-solution drivien, taking small parts and putting them together for bigger function your goal

numerical intergration

x=linspace(a,b,N) s=0; for i=1:N-1 rule= dx=x(i+1)-x(i) s=s+dx*rule; end int=s;

Q: x is least squares solution of y=A*x where A had many rows and few columns

x=pinv(A)*y;

Q: Show how you would pre-allocate the required vaibles to excucute the code faster assumer vector x exists. Why does code run faster?

y = zeros(size(x));

Q:Create a complex varible z=3a-j4b Create a varible s that is the complex conjugate of z. Calculate the magnitude and phase of z assume a and b exist

z=(3*a)-(j*4*b); s=conj(z); magnitude=abs(z); phase=angle(z);


Kaugnay na mga set ng pag-aaral

biology - recitation 4 - taste - quiz 2

View Set

7th grade SS Chapter 34 The Scientific Revolution

View Set

Political Partisipation Khan Academy

View Set

Unit 2-Principles of Imaging Part 2: CT, MRI; Intro to Fractures

View Set