create a text file named "student_id_q1.txt" with sine and cosine function as follows. sin( 1) + cos( 1) = 1.38 sin( 2) + cos( 2) = 0.49 sin( 3) + cos( 3) = -0.85 . . sin(120) + cos(120) = 1.39 in matlab

To create a text file named "student_id_q1.txt" with the sine and cosine function results in MATLAB, you can use the following code:

main.m
fileID = fopen('student_id_q1.txt', 'w');  % Open the file for writing

for i = 1:120
    sin_value = sin(i);
    cos_value = cos(i);
    result = sin_value + cos_value;

    fprintf(fileID, 'sin(%d) + cos(%d) = %.2f\n', i, i, result);
end

fclose(fileID);  % Close the file
275 chars
12 lines

This code uses a loop to calculate the sine and cosine values for each number from 1 to 120 and then writes the equation and the result into the text file. The fprintf function is used to format the output and write it to the file. The file is opened using fopen with the 'w' flag to open it for writing, and closed using fclose when finished.

After running this code, a text file named "student_id_q1.txt" will be created with the desired content.

Note: Make sure to change the file path if you want to save the file in a different directory.

related categories

gistlibby LogSnag