Freemat --> Plotting Two Curves on the same Plot

  • Thread starter Thread starter jedishrfu
  • Start date Start date
AI Thread Summary
A MATLAB program is presented to plot sine and cosine curves over the interval from 0 to 2π. The code utilizes the linspace function to generate 100 equally spaced points within this range, then computes the sine and cosine values for these points. The sine curve is plotted in blue, while the cosine curve is shown in red, both with increased line thickness for clarity. The program includes essential labels for the x and y axes, a title, and a legend to distinguish between the two functions. A grid is added to enhance visualization. The solution is credited to multiple contributors, including the original proposer and coder.
Messages
15,443
Reaction score
10,142
Here's a simple MATLAB program to plot the sine and cosine curves for the interval 000 to 2π2\pi2π:

Code:

Matlab:
% Define the interval from 0 to 2π
x = linspace(0, 2*pi, 100);

% Compute sine and cosine values
y_sin = sin(x);
y_cos = cos(x);

% Plot the sine and cosine curves
figure;
plot(x, y_sin, 'b', 'LineWidth', 2); % Sine curve in blue
hold on;
plot(x, y_cos, 'r', 'LineWidth', 2); % Cosine curve in red

% Add labels, title, and legend
xlabel('x (radians)');
ylabel('Function value');
title('Sine and Cosine Functions');
legend('sin(x)', 'cos(x)');

% Grid for better visualization
grid on;
hold off;

Explanation:​


  • linspace(0, 2*pi, 100): Generates 100 equally spaced points between 000 and 2π2\pi2π.
  • sin(x) and cos(x): Computes sine and cosine values for these points.
  • plot(x, y_sin, 'b', 'LineWidth', 2): Plots the sine curve in blue with a thick line.
  • plot(x, y_cos, 'r', 'LineWidth', 2): Plots the cosine curve in red with a thick line.
  • xlabel, ylabel, title, and legend: Add necessary labels and title.
  • grid on: Enhances visibility with a grid.

Output:

Screenshot 2025-03-16 at 12.03.09 AM.png


Solution Credits:

Proposed by: @hagopbul
Coded by: @ChatGPT
Tested on: Freemat a Matlab clone
Reviewed by: @jedishrfu
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
0
Views
789
Replies
0
Views
323
Replies
0
Views
310
Replies
8
Views
2K
Replies
1
Views
364
Replies
1
Views
2K
Back
Top