본문 바로가기

공부/프로그래밍

matlab(매틀랩) programing 강의 180411

2015b 버전 기준

여러가지 식을 보자.


continew 반복문을 점프하고 다음 반복을 한다.

break 반복문에서 탈출한다


------------------------------------------

x의 조건에 따라 값을 계산해 g에 할당한다.


x=-2*pi:0.01:2*pi;

>> n=length(x);



for i=1:n

if x(i)>=-pi&x(i)<=pi

g(i)=cos(x(i));

elseif x(i)<=-pi|x(i)>=pi

g(i)=-1;

end

end

>> plot(x,g)


------------------------------------------

1/e = (1-1/n)^n이 근사값과 실제값의 차이가 0.0001 미만이 될 때까지 n의 값을 순환하는 식


n=1;

error_e=1000;


while abs(exp(-1)-(1-1/n)^n)>0.0001

n

error_e


x(n)=n;

y(n)=(1-1/n)^(-n);


e_old=(1-1/n)^n'

n=n+1;

e_new=(1-1/n)^n;


error_e=abs(e_old-e_new);

end


plot(x,y)

hold on;

plot([0 n], [exp(1) exp(1)]);

hold off;


n


 z=(1-1/n)^(-n)


z =


    2.7184


-----------------------------------------

위와 같다.


for n=1:100000


error=exp(-1)-(1-1/n)^n;

if error<1e-6

break;

end

end

disp(n)

z=(1-1/n)^(-n);

disp(z);


----------------------------------------

갤런인지 리터인지 단위를 정하고 얼마나 살 지 정한 뒤 가격을 산출하는 식


z=input('gallon or liter? 1/2 = ');

y=input('how many? = ');

if z==1

    bill1=y*2.89;

elseif  z==2

        bill1=y*0.264*2.89;

end


    bill1

    

or


z=input('gallon or liter? 1/2 = ');

switch z

    case 1

    y=input('how many? = ');

    bill1=y*2.89;

    case 2

        y=input('how many? = ');

        bill1=y*0.264*2.89;

    otherwise 

        disp('unvailable value')

end


    bill1

    

728x90