plot a function using sine and cosine that will start at point [0,0], go to [2,6], from there to [8,0] a from there to [13,15] in matlab

You can use sine and cosine functions along with plot function to plot a curve that passes through given four points. Here's the code:

main.m
% Given points
points = [0 0; 2 6; 8 0; 13 15];

% Generate the required x-values using linspace
x_values = linspace(0, 13, 1000);

% Calculate the y-values using sine and cosine functions
y_values = zeros(1, length(x_values));
for i = 1:length(x_values)
    if x_values(i) <= 2
        y_values(i) = (6/2) * sin(pi*x_values(i)/2);
    elseif x_values(i) <= 8
        y_values(i) = -3*cos(pi*(x_values(i)-2)/12) + 3;
    else
        y_values(i) = (15/5)*(x_values(i)-8) + 0;
    end
end

% Plot the curve
plot(points(:,1), points(:,2), 'o', x_values, y_values);
563 chars
21 lines

The code first defines the four points that the curve should pass through. It then generates 1000 x-values evenly spaced between 0 and 13 using linspace. It then loops over each x-value, calculating the corresponding y-value using sine and cosine functions. Finally, it plots the four points using o marker style and the curve using plot function with the generated x and y values.

The resulting plot should pass through the four specified points.

related categories

gistlibby LogSnag