0

I have a matlab function that contain some constant parameter, I want to draw that function, on say same figure, using hold on (probably) while changing the value of that constant. This my code:

close all
clear all
clc
m = 5;
x = 1:1:10;
y = m*x + 10;
h1 = figure;
plot(x,y)
m = 10;
figure(h1);
hold on
plot(x,y,': r')

When I tried using this code, I got two lines coincident on each others; and it looks matlab just used last value for the parameter m how can I make it use different values. I found some stuff here, but doesn't fulfill my needs. Any suggestions?

3 Answers 3

1

You need to recalculate y as well:

m = 5;
x = 1:1:10;
y = m*x + 10;

h1 = figure;
plot(x,y); hold on;

m = 10;
y = m*x + 10;

figure(h1);
plot(x,y,': r')

Or create an anonymous function:

x = 1:1:10;
f = @(m) m*x + 10;

%// and then:
h1 = figure;
plot(x,f(5)       ); hold on;
plot(x,f(10),': r');
Sign up to request clarification or add additional context in comments.

3 Comments

all fine, however, in my actual situation, I want use that for PID control, where I've my parameters and ODE equations defined in one file (function file), and the parameters values and plots are defined in another m-file (the run file), you may check this for two links robot. Or should I post a new question? many thanks.
@AlFagera I cant open the link, but I'd guess it's definetely a new question.
@ thewaywewalk, Sorry for that; [this] (staff.kfupm.edu.sa/SE/mshahab/081_ee656_hw5_sol_shahab.pdf) , is the correct link.
0

Currently, you're only updating m but you also have to calculate y again. This is why it plots exactly the same y (i.e. m is still 5) function when you issue the second plot.

You might want to use a simple for loop for that, like:

m = 5;
x = 1:1:10;
figure;
hold on;
for m=1:1:10
    y = m*x + 10;
    plot(x,y,': r')
end

2 Comments

if I want to make legend in this case using for loop, how it'll work.
.. see my other answer.
0

In addition to the short answer - improving the plot..

    %% Data Preparations
    x = 1:10;
    ms = 3;             % number of different slopes

    %% Graph Preparations
    hold on;

    % Prepare the string cell array
    s = cell(1, ms);

    % Handle storage
    h = zeros(1, ms);

    % Plot graphs
    for m=1:ms
        y = m*x + 10;
        h(m)= plot(x,y,'Color',[1/m rand() rand()]);
        s{m} = sprintf('Plot of y(m=%d)', m);
    end

    % Plot all or select the plots to include in the legend
    ind = [ms:-1:1] .* ones(1,ms);  % plot all
    %ind = [ 1 3 4 ];               % plot selected

    % Create legend for the selected plots
    legend(h(ind), s{ind});

Additional advice: When working with MATLAB and you try to improve the performance of your code, you shoud try to avoid using for-loops since MATLAB is MATrix manipulation and that's what it can do best. Ones you've taken this philosophy in, you'll create the most beautiful code one-liners! ;)


This script is an adoption of Steve Lord's post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.