Freemat - Plotting Two Curves on the same Plot

  • Thread starter Thread starter jedishrfu
  • Start date Start date
Click For Summary
SUMMARY

This discussion provides a MATLAB program to plot sine and cosine curves over the interval from 0 to 2π using the Freemat environment, a MATLAB clone. The code utilizes the linspace function to generate 100 points, computes sine and cosine values, and employs the plot function to visualize the curves in distinct colors. Essential elements such as labels, titles, and a grid are included for clarity and enhanced visualization.

PREREQUISITES
  • Familiarity with MATLAB programming syntax
  • Understanding of trigonometric functions: sine and cosine
  • Knowledge of data visualization techniques in MATLAB
  • Basic grasp of plotting functions and graphical representation
NEXT STEPS
  • Explore advanced MATLAB plotting techniques, such as subplot for multiple plots
  • Learn about customizing plot aesthetics using set and gca functions
  • Investigate the use of Freemat for numerical computing and its differences from MATLAB
  • Study the implementation of animations in MATLAB plots to visualize dynamic changes
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and educators who wish to visualize mathematical functions effectively. It is particularly useful for those transitioning from MATLAB to Freemat or seeking to enhance their plotting skills.

Messages
15,606
Reaction score
10,369
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:
Technology news on Phys.org

Similar threads

  • · Replies 1 ·
Replies
1
Views
750
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
777
  • · Replies 1 ·
Replies
1
Views
762
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
891
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K