Question
matlab only

Question 5: a.) Write an m-file using conditional statements to evaluate the following function, assuming that the scalar var
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

60 50 40 30 20 10 -2 4 6 10

Add a comment
Know the answer?
Add Answer to:
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...
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