Question

Ql. The following MATLAB code computes the cosine value ranging from 0 to 10 using vectorization technique: t = 0:1:10; y cos

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given code:

clc;
clear;

t=0:1:10;
y = cos(t);
y
clc; clear; 1 2 3 4 5 6 7 t=0:1:10; Y = cos(t); Y

output:

y = 1.00000 0.54030 -0.41615 -0.98999 -0.65364 0.28366 0.96017 0.75390 -0.14550 -0.91113 -0.83907

Using for loop:

clc;
clear;

y = []; %empty vector to store cosine values.
for t=1:11 %it will run 1 to 11 so run 11 times.
y(t) = cos(t-1); %calculating consine values 0 to 10 and strong in vector.
endfor
%printing the cosine values.
disp("Using for loop: ")
y
1 clc; 2 clear; 3 4 y = []; $empty vector to store cosine values. 5 for t=1:11 fit will run 1 to 11 so run 11 times. 6 y(t) c

output:

Using for loop: y = 1.00000 0.54030 -0.41615 -0.98999 -0.65364 0.28366 0.96017 0.75390 -0.14550 -0.91113 -0.83907

Using while loop:

clc;
clear;

y = []; %empty vector to store cosine values.
t=1; %staring with 1 because vector index starts with 1.
while t<=11 %up to 11 ie. it will run 11 times.
y(t) = cos(t-1); %calculating cosine value 0 to 10 and storinng in vector.
t += 1; %increaseing the t value.
endwhile
%printing the consine values.
disp("Using while loop: ")
y

1 clc; 2 clear; 3 4 y = []; tempty vector to store cosine values. 5 t=1; $staring with 1 because vector index starts with 1.

output:

Using while loop: y = 1.00000 0.54030 -0.41615 -0.98999 -0.65364 0.28366 0.96017 0.75390 -0.14550 -0.91113 -0.83907

Note: my friend if you have any questions or queies comment below. I am happy to answer your all questions. i will sort out your queries. Thank you my friend.

Add a comment
Know the answer?
Add Answer to:
Ql. The following MATLAB code computes the cosine value ranging from 0 to 10 using vectorization...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT