Challenge

Project - Data Handling and Interpolation

Solution:
% Interp 1 (1D interpolation):
>> A=xlsread('Matlab_Project.xlsx'); % Read excel file in matlab program.
>> x=A(:,1); % Read the first column of the excel file.
>> y=A(:,2); % Read the second column of the excel file.
>> figure(1); % Add figure 1.
>> plot(x,y,'r'); % Plot the graph with red colour.
>> grid;xlabel('voltage');ylabel('current');title('Project'); % Formatting the graph window.
>> xq=[-17.3:0.1:0.9]; % Defining the given interval.
>> yq1=interp1(x,y,xq); % Linear interpolation.
>> yq2=interp1(x,y,xq,'spline'); % Spline interpolation.
>> figure(2); % Add figure 2.
>> plot(x,y,'b',xq,yq1,':.'); % Plot linear interpolation.
>> grid;title('Linear Interpolation'); % Formatting the graph window.
>> figure(3); % Add figure 3.
>> plot(x,y,'g',xq,yq2,':.'); % Plot spline interpolation.
>> grid;title('Spline Interpolation'); % Formatting the graph window.
>> fid=fopen('project_output1.txt','w'); % Creating a file id to open the text file & writing in it with write(w) permission.
>> fprintf(fid,'%f\n',xq,yq1,yq2); % Store the text data in an ouput file.
% Interp 2 (2D interpolation):
>> [X,Y]=meshgrid(-17.3:0.9,-17.3:0.9); % Generate X-Y grid for the range specified.
>> Z=Y.*exp(-X.^2-Y.^2); % Creating a function 'Z'
>> figure(1); % Add figure 1.
>> surf(X,Y,Z);title('original sampling'); % Surface plot.
>> [X1,Y1]=meshgrid(-17.3:0.1:0.9,-17.3:0.1:0.9); % Meshgrid
>> Z1=interp2(X,Y,Z,X1,Y1); % 2D interpolation.
>> figure(2); % Add figure 2.
>> surf(X1,Y1,Z1);title('Linear Interpolation using Finer grid'); % Surface plot.
>> [X2,Y2]=meshgrid(-17.3:0.1:0.9,-17.3:0.1:0.9); % Meshgrid
>> Z2=interp2(X,Y,Z,X2,Y2,'cubic'); % 2D interpolation.
>> figure(3); % Add figure 3.
>> surf(X2,Y2,Z2);title('Cubic Interpolation over a Finer Grid'); % Surface plot.
>> fid=fopen('project_output2.txt','w'); % Creating a filei d to open the text file & writing in it with write(w) permission.
>> fprintf(fid,'%f\n',Z1,Z2); % Store the text data in an ouput file.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.