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
  • In MATLAB The value of cos(x) can be approximated using a Maclaurin series + +... cos(x)=1-1...

    In MATLAB The value of cos(x) can be approximated using a Maclaurin series + +... cos(x)=1-1 2! 4! 6! Which can be expressed compactly as cos(x) = {(-1)+7 (2(k-1))! 00 2(k-1) k-1 Write Matlab code using a while loop that calculates cos(2) with 5 terms of Maclaurin series. Compare the value with the one calculated with a built-in function cos (2) in Matlab. The following is an expected output from your code: Using the Maclaurin series cos( 2.0) with 5...

  • Using matlab, write the code and plot the graph for the following equations. The x axis...

    Using matlab, write the code and plot the graph for the following equations. The x axis would be theta from 0 deg to 360 deg while the y axis would be angular or tangential position, velocity and acceleration. 10 100 E7360 AB- eD100mm sin 1O-Sn 2To ne cose, t Cas 2 3 4 O,= To (reano,re bease-feo, sene-Feo 6 as ) Cos O2 10 100 E7360 AB- eD100mm sin 1O-Sn 2To ne cose, t Cas 2 3 4 O,= To...

  • For questions 10-12, refer to the following iterative code computes values for the table t. 1 pub...

    For questions 10-12, refer to the following iterative code computes values for the table t. 1 public int [] tIterative (int [C A)1 2 3 4 int n - A.length; int [] t = new int [n]; int j; for (inti-0; i 0) while (j > 0 && A[j] [1] A[i] [O]) 12 13 14 15 16 t[i]Math.max( t[i-1] , A[i][i] - A[i] [o] + t[j] ); return t; 10. Does the code for tIterative use dynamic programming? 11. What...

  • THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that...

    THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that calculates exp(2) by a Maclaurin series using a while loop exp x )=-+-+-+-+ The while loop should add each of the series terms. The program error as defined below is smaller than 0.01. Error is the exact value (i.e. exp(2)), minus the approximate value (i.e., the current series sum) should exit the loop when the absolute absolute error-lexact-approx Use fprintf within the loop such...

  • 2. What is the value of x alter the following code is executed # include<iostream> using...

    2. What is the value of x alter the following code is executed # include<iostream> using namespace std; int main int t-0, c- 0,x-3; while (c < 4) t=t+x; cout << x return 0; a) 3 b) 21 c) 24 d) 48 3. What is the output of the following code? # include<iostream> using namespace std; int main0 int a- 3; for (int i 5; i >0; i) a a+i cout << a << “ “ return 0; d) 8...

  • Use the attached Matlab code as a basis to solve the following ordinary differential equation usi...

    Question 1 QUESTION 2 Use the attached Matlab code as a basis to solve the following ordinary differential equation using Euler's method, with timestep of 0.1, from t-0to t-100. d)0) -0 - sin (5vt cos(у Plot y versus t from t=0 to t=100. How many local maxima are on this interval(do not include end points). Be careful to count them all! Answer should be an integer 1 w% Matlab code for the solution of Module 2 3 dt-9.1; %dt is...

  • Consider the following code: int w- 10; while (w>0) scanf("%d", &y); if(y>0){ V++i else The value...

    Consider the following code: int w- 10; while (w>0) scanf("%d", &y); if(y>0){ V++i else The value stored in variable v at the end of the execution of the loop could best be described as the sum of all positive numbers scanned from the terminal the number of times the loop executed the sum of all non-positive numbers scanned from the terminal the number of positive integers scanned from the terminal the number of negative integers scanned from the terminal

  • You have the matrix below: a) 9 10 11 12 What will be displayed by the code: A(2:3,2:3) What will be displayed when you run the code below? b) a-0 while a<10 aa+3: end disp (a) d) Give the st...

    You have the matrix below: a) 9 10 11 12 What will be displayed by the code: A(2:3,2:3) What will be displayed when you run the code below? b) a-0 while a<10 aa+3: end disp (a) d) Give the steps to find the roots of the following linear equation using MATLAB. rs + 2x4-5x3 + 7x2 + 12x + 20 = 0 a) Give MATLAB commands to plot, on the same figure, the two functions: f(t) = 3t2 +2t-0.5 g(c)-2t...

  • Can you write hand solutions and matlab code please? Q3) There is a square wave with...

    Can you write hand solutions and matlab code please? Q3) There is a square wave with the period of T=21. The amplitude is 3 between 0 and it The amplitude is 0 between 1 and 2 T. 2 3 4 5 a) Find the Fourier series coefficients of this square wave. a = x()ật Il x(t)cos(kt)dt bx = ( x(t)sin(kt)dt b) Using Matlab, plot the truncated Fourier series; (1) For k 0 to 10 (II) For k 0 to 1000...

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