plot a function that will go from [0,0] to [5,8], from there it will go to [10,0] and then to [13,-4] using sine, cosine, logarithmic, quadratic and exponentional functions] in matlab

Here is an example code that plots the required function using the mentioned functions:

main.m
% defining the functions
x1 = linspace(0,5,50);
y1 = sin(x1);

x2 = linspace(5,10,50);
y2 = log(x2-4)+2;

x3 = linspace(10,13,50);
y3 = -2*(x3-10).^2+8;

% plotting the function
figure()
plot(x1, y1, 'b-', 'LineWidth', 2); hold on
plot(x2, y2, 'r--', 'LineWidth', 2); hold on
plot(x3, y3, 'g:', 'LineWidth', 2); hold on
plot([5 10],[8 0],'k', 'LineWidth', 2); hold on
plot([10 13],[0 -4],'k', 'LineWidth', 2)
xlabel('X-axis')
ylabel('Y-axis')
legend('sin','log','quadratic','location','northeast')
grid on
506 chars
22 lines

This code will generate a plot that starts from [0,0] and goes to [5,8] using sine function, from there it goes to [10,0] using logarithmic function and finally it moves to [13,-4] using quadratic function. The resulting plot will have all functions shown with different colors and line-types along with the points where the function changes its course.

related categories

gistlibby LogSnag