Вы находитесь на странице: 1из 2

Assignment 1

Problem 1: Obtaining the Minimum Value


The MATLAB Code for this problem is given below. The intent of this question was to ask
for maximum value of the problem. The minimum value is actually trivial, because it
happens at x = 0.
x=[0:0.07:5];
fval=x.^2.*exp(-x);

[fmin,i]=min(fval);
xmin=x(i);
disp(['Min value: ',num2str(fmin), ', at
x=',num2str(xmin)]);

[fmax,i]=max(fval);
xmax=x(i);
disp(['Max value: ',num2str(fmax), ', at x=',num2str(xmax)]);

Results (both results are accepted as correct answers):


1&2: Min value: 0, at x=0
1&2: Max value: 0.54122, at x=2.03

Problem 2: Vector Summation


function [evenSum,funVal]=vectFun(x)
n=length(x);
evenSum=sum(x(2:2:n));
funVal=sum(x.*[1:n]);
end

Explanation:
Note that x(2:2:n) gives all even-numbered terms of vector x.
Likewise, x(1:2:n) will give all odd-numbered terms.
For funVal, we note that 1:n yields a vector, which when multiplied with x gives:
1*x(1) + 2*x(2) + so.on...

Results
For the vector: x1=[7, 1, 4, 8, 7, 9, 6]:
3&4: evenSum: 18; funVal: 184
For the vector x2:
5&6: evenSum: -1.3442; funVal: -3.8729
Problem 3: Matrix Problem
For the given matrix:

Please try out the commands and you will get the results
>> sum(A(1:2:end,1))
ans =
0
>> rank(A(1:2,2:4))
ans =
2
>> b = A(2,2:5)
b =
1 3 -2 1
>> B = B = A(3:4,1:2)
B =
-1 0
-3 2

Problem 4: Plotting the data


1 6

4 (b)
0.5 (a) Error
2

0 0
0 0.2 0.4 0.6 0.8 1 0 1 2 3 4

6 6

4 (c) 4 (d)
2 2

0 0
0 1 2 3 4 0 1 2 3 4

6 6

4 (e) 4 (f)
2 2

0 0
0 1 2 3 4 0 1 2 3 4

The plots shown above clearly indicate that (d) and (e) are the right answers.

Вам также может понравиться