4rth semester matlab assignment solved 70 question

Post on 21-Jul-2016

78 Views

Category:

Documents

9 Downloads

Preview:

Click to see full reader

DESCRIPTION

solved questions

TRANSCRIPT

QUESTION: 1

Run the MATLAB code:a = 1:5; d = a + i * a; e = d' f = d.'Ans: >> a=1:5;>> d=a+i*a;>> e=d'e =1.0000 - 1.0000i2.0000 - 2.0000i3.0000 - 3.0000i4.0000 - 4.0000i5.0000 - 5.0000i>> f=d.'f =1.0000 + 1.0000i2.0000 + 2.0000i3.0000 + 3.0000i4.0000 + 4.0000i5.0000 + 5.0000iQUESTION: 3a = [1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ; 13 14 15 16]a = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

>> x = [ -5 -10 -15]x = -5 -10 -15

>>diagElement = diag (a)diagElement = 1 6 1116>>diagMatrix = diag (diag (a))diagMatrix = 1 0 0 0 0 6 0 0 0 0 11 0 0 0 0 16

QUESTION: 2 Given A =

1 2 41 1 12 3 1

and B =2 2 22 2 22 2 2Run the MATLAB code:Greater = A > BGreaterThanOne = A > 1

Ans:>> a = [1 2 4 ; 1 1 1 ; 2 3 1]a = 1 2 4 1 1 1 2 3 1b = [2 2 2 ; 2 2 2 ; 2 2 2]b = 2 2 2 2 2 2 2 2 2

greater = a > bgreater = 0 0 1 0 0 0 0 1 0

>>greaterThanOne = a > 1greaterThanOne = 0 1 1 0 0 0 1 1 0

QUESTION: 4Find a way to delete zeros from the vector x.

x = [0 6 0 7 3 0 3 0 2 2]x = 0 6 0 7 3 0 3 0 2 2>>x(x==0) = []x = 6 7 3 3 2 2

>>Dmatrix = diag (x)Dmatrix = -5 0 0 0 -10 0 0 0 -15

>>SuperdiagElement = diag (a,2)

SuperdiagElement = 3

QUESTION: 6Ans:>>name = upper('matlab')name =MATLAB

>>fun = strrep('hahahahahahahahahahahahahahahahaha','a','i')fun =hihihihihihihihihihihihihihihihihi

>> greet = 'Welcome'greet =Welcome

>>where = 'to Joan''s'where =to Joan's>>party = 'birthday party'party =birthday party>>final = str2mat(greet, where, party)final =Welcome to Joan's birthday party>>text = 'Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday'text =Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday>> [day,rest] = strtok(text, ',')day =Mondayrest =, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday>> [day2,rest] = strtok(rest, ',')day2 = Tuesdayrest =, Wednesday, Thursday, Friday, Saturday, Sunday

QUESTION: 5

Run the MATLAB code: x = [1/4 1 sqrt(2) .3]; y = sin(x); plot(y)

>> x = [1/4 sqrt(2) 3]x = 0.2500 1.4142 3.0000>> y = sin(x)y = 0.2474 0.9878 0.1411>> plot(y)

QUESTION: 7 The MATLAB code text(x,y,'alpha')Places the letter alpha at position (x, y). Write the text ‘\Graph of e-xsin 'at (.5, .2)

>> x=0:pi/40:2*pi;>> y=sin(x);>> plot(x,y)>> text(0.5,0.2,'graph of e^{-x}sin\phi')

QUESTION: 8Ans:>> A=[1 1;2 2;3 3;4 100] 1 1 2 2 3 3 4 100>> Average=mean(A)Average =2.5000 26.5000>> Med=median(A)Med = 2.5000 2.5000>> Dev=std(A)Dev =1.2910 49.0068

QUESTION: 9Ans:

>> A=[0 4 4;2 0 2;4 2 0];>> [Asend,Ind]=sort(A)

Asend = 0 0 0 2 2 2 4 4 4

Ind = 1 2 3

2 3 2 3 1 1 >> Decend=flipud(sort(A))

Decend = 4 4 4

2 2 2 0 0 0

QUESTION: 10Ans:

function [s] = sinm(x) s=sin(x)-2.*x+2;

endOn command window

>> sinm_2(0:2*pi/40:2*pi);>> fplot('sinm_2',[-10 10])

>> grid on>> title('the function sin(x)-2.*x+2')>> xzero=fzero('sinm_2',2)

xzero =

1.4987

QUESTION: 15 Ans: >> %Dec2Bin will convert a number to binary equivalent

>> a=dec2bin(34)

a =

100010QUESTION: 16Ans:function [] =etimef(~)ticnum_length=1:100000;tocend

QUESTION: 17 Ans:

>> c=0:20:100;>> %formula for changing degree Celsius into oF.>> F= (9/5)*c+32;>> e=c'

e = 0 20 40 60 80 100

>> g=F' g = 32

68 104 140 176 212>> convert_c_to_F=[e g]convert_c_to_F =

0 32 20 68 40 104 60 140 80 176 100 212(OR)

QUESTION: 17Ans:

function [F,X] =temp(C)C=([0:20:100])';X=9./5.*C+32;F=([X])';fprintf(' C F\n');fprintf(' (degree) (fahrenheight)\n');

fori=1:length(C);fprintf('%8g %16g\n', C(i),F(i));endend

QUESTION: 18. In Europe daylight time starts on the last Sunday of March and ends on the last Sunday of October. Write a function that determines whether a given day number is in the summertime period or in the wintertime period of the Daylight Saving Time

Ans:

function [f]= q18(datetime)

datetime= input('enter the date')[yr,mon,dom,hr]=datevec(datetime);dow=weekday(datetime)-1;f = 'day numer is in summer time';if (mon>4 &mon<10), return; end;if (mon==4 &dom>7), return; end;if (mon==4 &dom<=7 &dow==0 &hr>=2), return; end;if (mon==4 &dom<=7 &dow>0 &dom>dow), return; end;if (mon==10 &dom<25), return; end;if (mon==10 &dom>=25 &dow==0 &hr<2), return; end;if (mon==10 &dom>=25 &dow>0 & dom-24-dow < 1), return; end;f ='day number is in winter time';end

QUESTION: 21

Let be given the string `Need-to-split-this-string'. We want to break it into the -tive strings `Need', `to', `split', `this', and `string'. Solutions may be based on strtok and the much faster strread

Ans:function [parts] =q21( ~ )str='18c'splitstr='[c]';

parts=regexp(str,splitstr,'split');end

QUESTION: 22

We have a string that looks like `18C'. How to keep only the number 18?

Ans:function [Result,N] =q22(~)S='18c'N = sscanf(S, '%d')end

QUESTION: 23We want to save a vector v=[1 2 3 4]; into a text file. How to that?Ans:

V=[1 2 3 4]>> save textfile.m V_ascii

%to save the file>>load textfile.m>>textfiletextfile=1 2 3 4QUESTION: 24

Write a code that removes all 2's in a matrix A

Ans:

function [ new] = q24(matrix )

matrix=input('enter matrix')

new=matrix(matrix==2);endOR>> A=[2 2 6;3 2 6;2 7 4];>> A(A==2)=[]

A = 3 7 6 6 4

QUESTION: 25How can I comment several lines at once in stead of typing the symbol % at the beginning of each line?

Ans:>>if(0)I am a student.My name is Azeem.I want peace.end%The text between if(0) and end will become comments

QUESTION: 26How can I find where the matrix A changes sign?>> A=[1 -6 -4;-7 8 3]; >> S=sign(A)S =

1 -1 -1 -1 1 1

QUESTION: 28Given an array like [2; 8; 3; 30; 4; 50; 100; 200; 4; 80; 500]. I want to split it into three arrays with different ranges: [0-10), [10-100), and [100-1000).The above array should become

2; 8; 3; 4; 4

30; 50; 80

100; 200; 500

How to do this?

Ans:function [split,b ] = q28(~)% Result should look like 1 2 3 4...% 8 9 % 16 17 18 a = [1 2 3 4 8 9 16 17 18];b=a-(1:length(a));b = [true; diff(b(:)) ~= 0; true];split = mat2cell(a(:).', 1, diff(find(b)));

[m , n , o]=split{:}end

QUESTION: 30Is there a convenient way of listing the names of all *.m functions thatare called by a given *.m function?function [] =q30(n )n=input('enetr the name of fuction whose subfuctions names you want to know=')help 'n'end

QUESTION: 31 How can one set the background of a figure view as white?

Ans:

function [] =q31(~)

set(gca,'color','white')whitebg('white')end(OR)>>figure=figure(‘color’,[1 1 1])

QUESTION: 32How can one simulate a curve shaped like a heart by a spline function?Ans:function [] = q32(~)n=100;x=linspace(-3,3,n);y=linspace(-3,3,n);z=linspace(-3,3,n);[X,Y,Z]=ndgrid(x,y,z);F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);isosurface(F,0)lightingphongcaxisaxis equalcolormap('flag');view([55 34])end

QUESTION: 37 I want to index the entries in a matrix by using vectors with row and column indices. How to do that?

Ans:function [ out_args ] =q37( in_args)q=magic(4)q = [16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1]idx = sub2ind(size(q), [2 3 4], [1 2 4])idx = [ 2 7 16]end

QUESTION: 38

function [ output_args ] =q38( input_args ) k=10disp(fprintf('this is the number=%d',k))end

QUESTION: 39. I want to display variable values inside a disp command: \This is number: k". How to do that?

Ans:>> line([1,1],[2,0],'marker','*','color','r','linestyle',':')>> clf>> line([1,1],[2,0],'marker','*','color','r','linestyle',':')>> line([3,1],[2,4],'marker','+','color','g','linestyle','--')>> line([2,2],[3,4],'marker','>','color','b','linestyle','-')>> axis([-5 5 -5 5])

QUESTION: 41function [ output_args ] =q41( input_args )x1=-3:.2:3;x2=-3:.2:3;[X,Y] = meshgrid(x1,x2);f = (X-3).^2 + (Y-3).^2;g = 10.*(X + Y);ix = g >= 4;f(ix)=200;surf(X,Y,f)grid onend

QUESTION: 42How do I create a maximized figure window?function [ output_args ] =q42( input_args )gcf = q41set(gcf,'Units','normalized','Position',[0.00 0.034 1.00 0.905])end(OR)>>h=figure;>>pause (0.1)

>>Jf=get(h,’javaframe’);>>set(Jf, ‘maximized’ ,1);

QUESTION: 43I have to plot a variable number of data sets on the same graph with different symbols. How can I specify the symbols I want to use? How can I specify the order and color of the symbols to be used?

function [ output_args ] = q43( input_args )xlabel({'x red';'x1 green'})ylabel({'y red';'y1 green'})end

QUESTION: 44

function [ output_args ] = q44( input_args )p=[-100:100];q=1:sum(p>=1)p(q)endQUESTION: 45

function [ ] = q45(~)x=-1:.1:1;y=x;[xx,yy]=meshgrid(x,y);zz=(16-xx.^2).^(1/2);mesh(xx,yy,zz)title('The graph of z=(16-x^2)^(1/2)')xlabel('x-axis')ylabel('y-axis')zlabel('z-axis')axis tightend

QUESTION: 46I have a char array '000101'. Is there any way to split this into six separate elements: '0' '0' '0' '1' '0' '1'?

function [parts] =q46( ~ )str='0 0 0 1 0 1'

splitstr=[' '];parts=regexp(str,splitstr,'split');end

QUESTION: 47How can I store strings of variable length? [Hint use cell arrays]

function [ output_args ] = q47( input_args )userinput={'AJ48 NOT'; 'AH43 MANA'; 'AS33 NEWEF'};disp(userinput{1});end

QUESTION: 48function [ output_args ] =q48( input_args )fid = fopen('use.m', 'r')x = fread(fid,inf)end

QUESTION: 50>> x=1:4;>> y=1:10;>> c=find(ismember(x,y))

c =

1 2 3 4

>> %where 1 2 3 4 are inices of matching entries(OR)function [ output_args ] =q50( input_args )a =[2 3 1 2]b=[2 1 7 2]ind=find(a==b)end

QUESTION: 51function [ output_args ] =q51( input_args )a=[1 2 -3 ; 0 9 0 ; 0 8 -1];

a(find(a<0))=[0]end

QUESTION: 52function [ output_args ] = q52( input_args )A=[1 2 ; 3 4 ; 5 6; 6 7; 8 9 ; 5 4; 7 8; 8 0 ; 3 2; 1 1; 2 2; 3 3; 4 4; 5 6; 7 8]newmatrix=[A(1,:);A(5,:) ; A(10,:);A(15,:)]end

QUESTION: 53function [ output_args ] =q58( input_args )files = dir('*.txt');fori=1:length(2)eval(['load ' files(i).name ' -ascii']);endend

QUESTION: 54function [ output_args ] = q59( input_args )A=[1 2 3 4 5; 6 7 8 9 10]A=reshape(A,1,10)selectfirsteight=A(1:8)end

QUESTION: 55function [ output_args ] = q55( input_args )F=[1 2 3; 4 5 6; 7 8 9]A=[3 -2 1]; B=[6 9 -4];C=[-7 -5 2];a=cat(3,A,B,C)A = [0 -a(3) a(2); a(3) 0 a(1); -a(2) a(1) 0]crossproduct=cross(A,F)end

QUESTION: 56function [ output_args ] = q56( input_args )a = [1 2 3]'

b = [2 4 7]'div=a./bwhy=[1 2 3]/[2 4 7]end

QUESTION: 58function [ output_args ] =q58( input_args )load penny;figure;contour(flipud(P))colormap('Copper')end

QUESTION: 58 (a)function [ output_args ] =q58a( input_args )Z = peaks;[C,h] = contourf(Z,10);text_handle = clabel(C,h);colormap([1 1 0])set(text_handle,'BackgroundColor',[1 1 0],... 'Edgecolor',[1 1 0])caxis([-20 20])title({'Filled Contour Plot Using','contourf(Z,10)'})end

QUESTION: 59function [ output_args ] = q59( input_args )end

QUESTION: 60function [ output_args ] = q60( input_args )set(0,'defaulttextinterpreter','latex')end

QUESTION: 61function [ output_args ] = q61( input_args )x=[1 2 3 4 ]plot(x,cos(x),'-ro',x,sin(x),'-.b')list={'Label 1','Label 2'};leg=legend(list);

set(leg,'Textcolor',[1 1 0])end

QUESTION: 62function [ output_args ] = q62( input_args )set(gca,'color','none')whitebg('blue')whitebg([0 .5 .6])endQUESTION: 63function [ output_args ] = q63( input_args )set(0,'defaulttextinterpreter','latex') x=[1 2 3 4]; y=[ 2 5 6 7]x1=[6 3 7 8]x2=[ 0 9 7 6]plot(x,y,'-og',x1,x2,'--r>')end

QUESTION: 64function [ output_args ] = q64( input_args )tcell = {[1,0], [1,2,3], [3,2,1,4]};lens = cellfun('length', tcell);rmat = NaN(length(tcell), max(lens));fori = 1:length(tcell)rmat(i, 1:lens(i)) = tcell{i}end

QUESTION: 65function [ output_args ] =q65( input_args )c={'Bob', 'Mary', 'Fred','Ken'}s = fprintf(' %s %s \n' , c{1} ,c{3} );end

QUESTION: 66function [ output_args ] = q66( input_args )%calllib('libname', 'funcname', arg1, ..., argN)loadlibrary('MatlabGenericDll','GenericDll.h')calllib('MatlabGenericDll', 'FormatDrive')unloadlibraryMatlabGenericDll

end

QUESTION: 67

Solve the equation ax2+ bx + c = 0 symbolically for x, and next for b.

unction [ output_args ] = q67( input_args )syms x a b csolve('a * x ^ 2 + b * x + c=0','x')solve('a * x ^ 2 + b * x + c=0','b')end

QUESTION: 68. Solve the equation [cos (2x) + sin(x) - 1 = 0 ]symbolically. Next use ezplot to verify the result

function [ output_args ] = q68( input_args )syms x solve('cos(2*x) + sin(x) - 1 = 0','x')ezplot('cos(2*x)', -2, 3)hold onezplot('1-sin(x)', -2, 3)xlabel('cos(2*x)')hold offend

QUESTION: 69

Solve the equation tan(x) + sin(x) - 2 = 0 symbolically. Next, determine the numerical values of the roots.

>>syms x>>f=tan(x)=sin(x)-2>>solve(f);>>sol=sub(f,x,1.5)Sol=2.168

(OR)

function [ output_args ] = q69( input_args )

%You can find this solution by calling the MuPAD numeric solver directly and specifying the interval where this solution can be found. To call MuPAD commands from the MATLAB Command Window, use the evalin or feval functionsolve('tan(x)+sin(x)-2=0','x')evalin(symengine, 'numeric::solve(tan(x)+sin(x)-2, x = 0..4)')end

top related