MATLAB Solutions

MATLAB Solutions HNGU March-April 2023

MATLAB Solutions HNGU March-April 2023

P1 Write Expected returns of following commands or function


menu: This function displays a list of choices and prompts the user to select one. The function returns the index of the selected choice.

disp: This function displays its input argument(s) on the command window or console. There is no explicit return value from this function.

fclose: This function closes an open file. There is no explicit return value from this function.

%f: This is a format specifier used with fprintf and related functions to format a floating-point number with a fixed number of decimal places. There is no explicit return value associated with this specifier.

Nested for loop: This is a control structure used to iterate over multiple dimensions of an array or perform nested operations. There is no explicit return value associated with a nested for loop.

fopen: This function opens a file and returns a file identifier that can be used to read or write to the file. The function returns -1 if the file cannot be opened.

While loop: This is a control structure used to repeat a block of code while a condition is true. There is no explicit return value associated with a while loop.

continue statement: This statement is used to skip the current iteration of a loop and move on to the next iteration. There is no explicit return value associated with a continue statement.

break statement: This statement is used to exit a loop prematurely. There is no explicit return value associated with a break statement.

fprintf: This function writes formatted data to a file or the command window. The function returns the number of characters written.

factor: This function returns the prime factors of an integer in a vector.

isprime: This function checks whether an integer is prime and returns a logical value (true or false).

gcd(a,b): This function computes the greatest common divisor of two integers a and b.

If-loop: This is a control structure used to execute different code depending on whether a condition is true or false. There is no explicit return value associated with an if statement.

eig: This function computes the eigenvalues of a matrix.

ode23: This function is a numerical solver for ordinary differential equations (ODEs) using the second and third order Runge-Kutta formulas. It returns the solution to the ODE as a vector.

P2 Using input Method writes a program to find first 10 number multiples of 13


n = input('Enter a number: ');
for i = 1:10
multiple = 13 * i;
fprintf('%d x %d = %d\n', n, i, multiple);
end



P3 WAP to generate five even numbers between 5 and 15 in text file using fopen.


% Open a new text file for writing
fileID = fopen('even_numbers.txt', 'w');
% Generate five even numbers between 5 and 15
numbers = randi([3,7],1,5)*2;% random numbers in 1 cross 5 matrix between 3 and 7 so that its twice is even and between 5 and 15
% Write each number to the file
for i = 1:5
fprintf(fileID, '%d\n', numbers(i));
end
% Close the file
fclose(fileID);

P4 WAP to suppose x has five unoformly distributed valuse between -2 and 2
represents the values of x in following formats(fid=0, i.e. standard screen out put)
(a) defult format
(b) signed value and new line (i.e.: sign + or -)
(c) exponential format and new line

(a)
x = randi([-2 2], 1, 5);
fprintf('%d ', x);
Out Put will be 1.882102 -1.085021 -1.677056 1.937044 -0.406162
(b)
x = randi([-2 2], 1, 5);
fprintf('%+f\n', x);
Out Put will be-1.332794
-0.032430
+0.374689
-0.319822
+1.535767
(c)
x = randi([-2 2], 1, 5);
fprintf('%e\n', x);
Out Put will be -7.178166e-01
+5.028557e-01
-1.322584e+00
-6.293738e-01
+1.882102e+00


P5 WAP to illustrate the use of pause command to defining two matrices
and find its sum and multiplication and its square.


A = [1 2 3; 4 5 6; 7 8 9];
B = [9 8 7; 6 5 4; 3 2 1];
disp('Matrix A:');
disp(A);
disp('Matrix B:');
disp(B);
% Pause for 10 seconds
pause(10);
C = A + B;
disp('Sum of matrices A and B:');
disp(C);
% Pause
pause();%press any key
fprintf('\n Press any key!');
D = A * B;
disp('Multiplication of matrices A and B:');
disp(D);
pause(5);
E = D^2;
disp('Square of the multiplication:');
disp(E);


P6 What is use of %6.3 f and %0.3 f in fprintf?


x = 12.34567;
fprintf('%0.3f', x) % Output: "12.346"
x = 12.34567;
fprintf('%10.2f', x) % Output: " 12.35"
fprintf('%0.7f', x) % Output: 12.3450000
%m.n f where m non zero
Then total m character (including white space) and n decimal point
If m zero then it turns to n decimal point with out white space
Actually it use 0 insted of white space
Means %0.3 f
Print values 3 decimal place
Case 1
2.3 then 2.300
Case 2
4.567 then 4.567
Case 3
6.4567 then 6.457



See Also
Prac.-63 Create String Data in Text file from MATLAB.
% WAP to Create Text File and Write Some Data and overwritein this File.
clc; clear all; close all;
fid=fopen('Todays_File.txt','w');
fprintf(fid,' This is my first first notepad file which is created without openning notepad \n So, I am Happy to write this in MATLAB ');
fclose(fid);
fid=fopen('Todays_File.txt','a');
fprintf(fid,' \n add this in last line ');
fclose(fid);
Prac.-64
Read String Data in MATLAB from Text File.
clc;clear all;close all;
fid=fopen('Todays_File.txt','r');
My_data=textscan(fid,'%s','delimiter','\n');
fclose(fid);
Out Put
>>My_Data
>>My_Data{1}
>>My_Data{2}
>>My_Data{3}
Prac.-65
Create Numerical Data in Text file from MATLAB.
clc;clear all;close all;
x=-10:1:10;
y=x.^3;

fid=fopen('Numerical_Data2.txt','w');
for line=1:length(x)
fprintf(fid,'%d %d \n',x(line),y(line));
end
fclose(fid);
Out Put
Prac.-66
Read Numerical Data in MATLAB from Text File.
clc;clear all;close all;
fid=fopen('Numerical_Data2.txt','r');
My_Num_Data2=textscan(fid,'%d %d','delimiter','\n');
fclose(fid);
Out Put
>> My_Num_Data2
>>x= My_Num_Data2{1}
>>y= My_Num_Data2{2}
Prac 67
Obtain the Numerical Solution of the following first order differential Equation dx/dt=-2x with initial condition x(0)=1, for t between 0 and 10 using ode45 solver. Soln:
Write Following code in Script-M File and Save it as name func1.m
function xdot=func1(t,x)
xdot=-2*x;
end
Write Following code in Command Window
>> tspan=[1,10];
>> x0=1;
>> [t,x]=ode45('func1',tspan,x0);
>> plot(t,x)
>> grid on
>> xlabel('X-axis');
>> ylabel('Y-axis');
>> title('Graph of Solution')
Out Put
Graph

No comments: