Hi!

I am new to here. My friend show me this useful website. I hope anyone here could help me out. Ive tried to learn MATLAB. When i test it with my lecturer's file, i didnt score full mark. Please guide me through.

build-in function-functask2=sinh(x)-x.^2+x+1, xvalues=[1x50 double].

1) i need to compute the function at these xvalues and store in yvalues.

2) find best fit,quadratic n cubic to these values. Store the polynomial coefficient in linecoef, quadcoef and cubiccoef.

3) find new vector of x values,xnew,where there are 5 equispaced points betw each of the values in xvalues.

4) evaluate the polynomials and the function at xnew, store in yline, yquad, ycubic and yfunc.

5) do a cubic spline interpolation betw xvalues, yvalues, to find output values ynew at xnew.

6) overlay all 5 curves in a single plot.

my file is:

function out2=task2(in2)

%%% Compute what is originally in the workspace for task2.

out2=in2;

%%% Let x be a cell array containings the xvalues.

%%% Evaluate the function at x and compute them

%%% in yvalues.

x=in2.xvalues;

out2.yvalues=eval(in2.functask2);

%%% Compute the best fit straightline,

%%% quadractic and cubic to those yvalues.

%%%

out2.linecoef= polyfit(x,out2.yvalues, 1);

out2.quadcoef=polyfit(x,out2.yvalues,2);

out2.cubiccoef=polyfit(x,out2.yvalues,3);

r=numel(x);

xnew=linspace(x(1),x(end),6*(r-1)+ 1);

out2.xnew=xnew;

out2.yline=polyval(out2.linecoef,xnew);

out2.yquad=polyval(out2.quadcoef,xnew);

out2.ycubic=polyval(out2.cubiccoef,xnew);

x = xnew;

out2.yfunc=eval(in2.functask2,xnew);

ynew=spline(in2.xvalues, out2.yvalues, x);

out2.ynew=ynew;

figure(2); clf reset;

plot(xnew,ynew,'b', xnew,out2.yline, 'g',...

xnew,out2.yquad, 'y', xnew, out2.ycubic, 'k',...

xnew, out2.yfunc, 'm');

title( in2.functask2);

legend('vector', 'best fit line', 'quadratic',...

'cubic', 'function');

My up-2-date file is:

function out2=task2(in2)

out2=in2;

x=in2.xvalues;

out2.yvalues=eval(in2.functask2);

% To find the best fit straightline, quadratic

% and cubic at yvalues.

% Store them as polynomial coefficients in linecoef,

% quadcoef and cubiccoef respectively.

out2.linecoef= polyfit(x,out2.yvalues, 1);

out2.quadcoef=polyfit(x,out2.yvalues,2);

out2.cubiccoef=polyfit(x,out2.yvalues,3);

% To find a new vector, xnew.

% xnew contains values in x.

% xnew has 5 equispaced points between

% each of the values in x.

[x_val,y_val]= size(x);

% x_val the number of rows and y_val the number

% of columns in x.

aa=cell(1,(y_val-1));

% creates a 1-by-(y_val-1) cell array of empty matrices.

for i=1

y_val-1)

if i==1

aa{i}=linspace(x(i),x(i+1),7);

else % if i is not equal to 1

xx=linspace(x(i),x(i+1),7);

xxx=xx(2:end);

aa{i}=xxx;

end

end

xnew=[aa{1:end}];

out2.xnew=xnew;

% To evaluate the polynomials and functask2 at xnew.

% store them in yline, yquad, ycubic and yfunc

% respectively.

out2.yline=polyval(out2.linecoef,xnew);

out2.yquad=polyval(out2.quadcoef,xnew);

out2.ycubic=polyval(out2.cubiccoef,xnew);

x = xnew;

out2.yfunc=eval(in2.functask2,xnew);

% To perform cubic spline interpolation between

% xvalues and yvalues.

% computes the values at xnew and store them at ynew.

ynew=spline(in2.xvalues, out2.yvalues, xnew);

out2.ynew=ynew;

% To plot a graph on figure(2) showing the 5 curves.

figure(2); clf reset;

plot(xnew,out2.ynew,'b', xnew,out2.yline, 'g',...

xnew,out2.yquad, 'y', xnew, out2.ycubic, 'k',...

xnew, out2.yfunc, 'm');

title(in2.functask2);

legend('vector', 'best fit line', 'quadratic',...

'cubic', 'function');

cheers!! please help me out.

ZUs.