Please help. why is this not graph not working. I'm trying to print the graph of y'' + y' + y = 0 and y'' + y' + y = cos(x)

1 view (last 30 days)
x = linspace(0,10,100)
y = exp(-x/2)*cos((3^(1/2)*x)/2) + exp(-x/2)*sin((3^(1/2)*x)/2)
y1 = exp(-x/2)*cos((3^(1/2)*x)/2) + exp(-x/2)*sin((3^(1/2)*x)/2) + sin(x)
plot(x, y, 'o', 'MarkerEdgeColor', 'k', ...
'MarkerFaceColor', 'g', ...
'MarkerSize', 4)
hold on
plot(x, y1)
title('exp(-x/2)*cos((3^(1/2)*x)/2) + exp(-x/2)*sin((3^(1/2)*x)/2) + sin(x)')
legend('homogeneous','inhomogeneous')
xlabel('x')
ylabel('y')

Answers (1)

Star Strider
Star Strider on 12 Mar 2017
You need to vectorise your equations.
Also in the plot title, you need to enclose the values in curly brackets ‘{}’ that you want included in special operations, here exponents.
This works:
x = linspace(0,10,100);
y = exp(-x/2).*cos((3^(1/2).*x)/2) + exp(-x/2).*sin((3^(1/2)*x)/2)
y1 = exp(-x/2).*cos((3^(1/2)*x)/2) + exp(-x/2).*sin((3^(1/2)*x)/2) + sin(x)
plot(x, y, 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'g', 'MarkerSize', 4)
hold on
plot(x, y1)
title('exp(-x/2)*cos((3^{1/2}*x)/2) + exp(-x/2)*sin((3^{1/2}*x)/2) + sin(x)')
legend('homogeneous','inhomogeneous')
xlabel('x')
ylabel('y')
For more information on vectorising your code, see the documentation for Array vs. Matrix Operations.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!