This document contains MATLAB code for various looping and conditional structures including for loops, while loops, if/else statements, and switch statements. It also contains examples of plotting, input/output, and animating loops. Some key examples include:
1. A for loop that plots a function over a range of x values and animates the plot by changing the viewing angle every 0.1 seconds.
2. A comparison of using a for loop versus vectorization to calculate the sum of 1/n^2 from 1 to 1e8, showing the vectorized method is much faster.
3. Using a while loop to iteratively find a prime number entered by the user.
4. Examples
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
23 views1 page
Session 5:: %% Input
This document contains MATLAB code for various looping and conditional structures including for loops, while loops, if/else statements, and switch statements. It also contains examples of plotting, input/output, and animating loops. Some key examples include:
1. A for loop that plots a function over a range of x values and animates the plot by changing the viewing angle every 0.1 seconds.
2. A comparison of using a for loop versus vectorization to calculate the sum of 1/n^2 from 1 to 1e8, showing the vectorized method is much faster.
3. Using a while loop to iteratively find a prime number entered by the user.
4. Examples
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1
Session 5: Script
%% input------------------- for az = -30: 10 : -30+360
a=input('please enter a value (sinh):'); mesh(z); G=sinh(a) % (exp(x) - exp(-x))/2 view([az, 30]) disp(['the answer is: ', num2str(G)]) pause(0.1) end format short %% Series [Loop vs Vectorization!]----- a = 0.3333 % Sigma (1/n^2) -> pi^2/6 G = 1/3 S1 = 0; a == G N = 1:1e8; % 1e8: 6 digits precision %% fprintf------------------- tic clc for ii = N fprintf('Helo\n program is ready to S1 = S1 + 1/ii^2; start\n') end a = input('Enter a number: '); t1 = toc P = primes(a); format long G = max(P); S1; pi^2/6; fprintf('\nmax prime number < %g is: %g. tic \n',a,G) S2 = sum(1./N.^2); %% Plotter! [eval] t2 = toc clc, clear t1/t2 fprintf('*** PLOTTER ***\n\n ') %% While Loop------------------- f = input('Please enter: f(x)= ','s'); clc a = input(' min x: '); % while: Iteration is known G = input(' max x: '); i = 0; x = a : 0.1 : G; while i < 5 y = eval(f); % Evaluation f(x) String i = i + 1; plot(x,y) disp(i) end %% fprintf: Write a TEXT file %% While: Iteration in NOT known clc x = 4; x = -2:.1:2; while ~isprime(x) y = exp(x); disp('try again') A = [x; y]; x = input('Enter a number:'); %Id = fopen(filename, permission) end Id = fopen('Exp.txt','w'); disp('Prime found!') % r: read, % w: write, % a: append, % fprintf(FileId, Format, A) %% While: Iteration in NOT known----- fprintf(Id,'Exp of %g is: %g \r\n', A); % Iterations to get 0.01% precision \r:Carriage,Retrun, \n: NewLine,\r\n:Enter %% Condition: if, switch------------- %% Loop And Conditions-------- n = input('Input a Number:'); % 1-for, 2-while if rem(n,2) == 0 for i = 1 : 0.5 : 3 % index = values fprintf('%g is even\n', n) disp(i) % statements else end fprintf('%g is odd\n', n) n = 5; end f = 1; for i= 1:n %% Switch------------------ f = i*f; % factorial % one Conditional var and multiple end conditions fprintf('%g!= %g\n',n, f) % One case is true, switch break %% for: Cell------------------------- A = {'Ali', 'Zahra', 'Hasan', 'Reza'}; % switch x for j = 1:length(A) % case cond1 disp(['Name (', num2str(j),'): ', % S1 A{j}]) % case {cond1, cond2} end % S2 % otherwise %% for: Animate % S2 z = peaks; % end