MATLAB Script (Run it as a script, not from command window):
close all
clear
clc
fprintf('y(x = -5) = %.8f\n', func(-5))
fprintf('y(x = 3) = %.8f\n', func(3))
fprintf('y(x = 15) = %.8f\n', func(15))
x = input('Enter a value for x: ');
fprintf('y(x = %f) = %.8f\n', x, func(x))
x = -2:0.01:10;
y = zeros(1, length(x));
for i = 1:length(x)
y(i) = func(x(i));
end
plot(x, y), xlabel('x'), ylabel('y')
function y = func(x)
if x < -1
y = 3*exp(x + 1);
else
if x < 5
y = 2 + cos(pi*x);
else
y = 10*(x - 5) + 1;
end
end
end
Output:
y(x = -5) = 0.05494692
y(x = 3) = 1.00000000
y(x = 15) = 101.00000000
Enter a value for x: 2.5
y(x = 2.500000) = 2.00000000
Note: You need to enter some value for x when prompted and then at the end you get the plot.
Plot:
matlab only Question 5: a.) Write an m-file using conditional statements to evaluate the following function, assuming that the scalar variable x has a value. The function is for x <-1 - 3e y=2...
10) you will run/call the function with these given values. (b) Write a flow chart and the script file using conditional statements with logical operator if/else and if/elseif, to evaluate the following function, assuming that the scalar x has a value: y 2 sin(m) for-1 x < 5 y=10(X-5) + 1 for x 25 Use your file to evaluate y for x5,x 3 and x 15 by hand 10) you will run/call the function with these given values. (b) Write...
3. (a) Write a flow chart and the script file using for loop and conditional statements to evaluate and plot the folowing function, assuming that the salar x has a valiue: for x <-1 ys20yx + 6 y = 8x sin(2πχ) for-1 < x < 5 y=15(x-1) + 1 for x 25 3. (a) Write a flow chart and the script file using for loop and conditional statements to evaluate and plot the folowing function, assuming that the salar x...
Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following math function with x the input argument and y the output y(x)=0.8x4-13x2-5x The function should work for x being a scalar or a vector. Write a script file named PBTask4pl.m to a) Use the function to calculate y(3) and y(5) and display the results in command window b) Use the function to make a plot of the function y(x) for -5:5: x 5:5. Label...
2. (aj An object thrown vertically with a speed vo reaches a height h at time t,where with a user defined function that computes the time required to reach a Write a MATLAB program specified height h, for a given value of vo. The function's imput should be h, we and g Testverurtrott" for the case where h-100 m, vee50 m/s and g-9,82 /by free hand. Write the MATLAB you will run/call the function with these given values (b) Write...
1. Write a MATLAB function that takes a matrix, a row number and a scalar as arguments and multiplies each element of the row of the matrix by the scalar returning the updated matrix. 2. Write a MATLAB function that takes a matrix, two row numbers and a scalar as arguments and returns a matrix with a linear combination of the rows. For example, if the rows passed to the function were i and j and the scalar was m,...
2. (a) An object thrown vertically with a speed vo reaches a height h at time t, where: he vot tgt Write a MATLAB program with a user defined function that computes the time required to reach a specified height h, for a given value of vo. The function's input should be h, Vo, and g. Test your function for the case where h 100 m, Vo-50 m/s and g9,81 m/s2 by free hand. Write the MATLAB command how you...
Matlab please Using the X and Y values below, write a MATLAB function SECOND_DERIV in MATLAB. The output of the function should be the approximate value for the second derivative of the data at x, the input variable of the function Use the forward difference method and interpolate to get your final answer. X=[1,1.2,1.44,1.73,2.07,2.49,2.99,3.58,4.3,5.16,6. 19,7.43,8.92, 10.7,12.84,15.41,18.49]; Y=[18.89,19.25,19.83,20.71,21.96,23.6,25.56,27.52,28.67,27.2,19.38,-2.05,-50.9,-152.82,-354.73,-741.48,-1465.11];
Using the X and Y values below, write a MATLAB function SECOND_DERIV in MATLAB. The output of the function should be the approximate value for the second derivative of the data at x, the input variable of the function. Use the forward difference method and interpolate to get your final answer. X=[1,1.2,1.44,1.73,2.07,2.49,2.99,3.58,4.3,5.16,6.19,7.43,8.92,10.7,12.84,15.41,18.49]; Y=[18.89,19.25,19.83,20.71,21.96,23.6,25.56,27.52,28.67,27.2,19.38,-2.05,-50.9,-152.82,-354.73,-741.48,-1465.11];
MATLAB Problem HW7P2 (20 points) (5 pts) Write a user-defined MATLAB function called HW7P2_fn for the following math function 3 o-0.47x The input to the function is x and the output is y. Write the function such that x can be an array (use element-by-element operations) (15 pts) Use the function in (a) the command window to calculate y(-2) and y(5) (b) a script file HW7P2.m to determine y(x) for 0.001 Sx S 10 with 1000 points. Hint: Use the...
1. the value of expression 18 % 5 is _? 2. write the conditional statements in C++ to solve the following problem. declare variables properly. a. get a number for a month from the keyboard b. if a month is 1,2,3, print out "quarter 1" to the monitor c. if a month is 4,5,6, print out "quarter 2" to the monitor d. if a month is 7,8,9, print out "quarter 3" to the monitor e. if a month is 10,11,12,...