-------------------------------------------------- Question 1: -------------------------------------------------- >>clear; syms x; a=solve(x^2+4) a= 2i -2i >>b = char('a' - 30) b= C >>ints = linspace(0,10,11); >>c = sum(ints(mod(ints,2)==ones(1))) c= 25 >>data.s = { 'hi' }; >>data.s(2) = { data(1).s }; >>d = data.s{2} d= 'hi' -------------------------------------------------- Question 2: -------------------------------------------------- function stuff = prob2 % Solve for a root of x^4 - c1*x^2 -c2*x - c3 % Initialize Values c1=5; c2=22.5; c3=1.5625; % set constants in equation eps = 0.001; % tolerance inc = 1; % initial increment maxiter = 1000; % max iterations allowed iters=0; % iterations so far % Prompt user for initial value % Ensure $x$ is positive and real x = input('Enter initial value of pile depth: '); while x <= 0 | ~isreal(x) % while ~(x > 0 & isreal(x)) okay as well x = input('That value is "evil". Re-enter initial value of root: '); end % get initial value of $lhs$ to see if guess was good lhs = rhs(x,c1,c2,c3); while (abs(lhs) > eps) & (iters < maxiter) % decrease increment if sign changes if lhs*rhs(x+inc,c1,c2,c3) < 0 inc=inc/10; end % update x, f(x), and iteration count x = x + inc; iters = iters + 1; lhs = rhs(x,c1,c2,c3); end if iters == maxiter disp('Too many iterations...restarting!'); eval('prob2'); % Repeat the analysis return; % return without passing values end % Store final value of root, value of f(x) for the root, and iterations stuff = [x lhs iters]; % Subfunction function lhs = rhs(x,c1,c2,c3) %Computer left-hand side $lhs$ of equation lhs= x^4 - c1*x^2 - c2*x - c3; -------------------------------------------------- Question 3: -------------------------------------------------- function v = vec2set(v) ii = 1; while ii < length(v) firstelem = v(ii); rest_of_v = v(ii+1:end); rest_of_v = rest_of_v( firstelem ~= rest_of_v ); v = [v(1:ii) rest_of_v]; ii = ii + 1; end function result = set_union(x, y) result = vec2set([x y]); function result = set_intersect(x, y) x = vec2set(x); y = vec2set(y); ii = 1; result = []; while ii <= length(x) if any(x(ii)==y) % also acceptable: if sum(x(ii)==y)>0 result = [result x(ii)]; end ii = ii + 1; end % distribute.m x = [1 2 3 1]; y = [2 2 4]; z = [0 2 1 3]; x = vec2set(x); y = vec2set(y); z = vec2set(z); LHS = set_union(x,set_intersect(y,z)); RHS = set_intersect(set_union(x,y),set_union(x,z)); if LHS==RHS disp('Success! The law worked. Hooray!'); else disp('Failure! The law failed. Bummer!'); end -------------------------------------------------- Bonus Questions: -------------------------------------------------- Bonus 1: Daisy Niranjan Andy Michael Rohit Raju Paradee Maxim Bonus 2: >>data.s={'hi'}; >>data.s(2)={data(1).s}; >>sum(data.s{2}{1}(1:2)) ans = 209 Bonus 3: finds intersection of two vectors