Creating a MATLAB Plot with a varying variables?

47 views (last 30 days)
Hey guys,
I have been asked by my organization to make a plot of the function y= x^m on MATLAB. I essentially have to use Matlab to plot the function f(x) = x^m for m = 1,2,3, and 4 on the same figure, as x runs from 1 to 2.
This is what I have so far:
clear all;
close all;
m= 1:1:4;
x=1:0.1:2;
y = x.^m;
xlabel ('value of x'); ylabel ( 'function value');
hold on plot(x,y);
However, when I execute the script, it returns the error "Matrix dimensions must match". Anyone know an alternative route?

Answers (2)

Walter Roberson
Walter Roberson on 20 Sep 2015
y = bsxfun(@power, x.', m);

Matt J
Matt J on 20 Sep 2015
Edited: Matt J on 20 Sep 2015
x=1:0.1:2;
for m= 1:4;
xy{1,m}=x;
xy{2,m} = x.^m;
end
plot(xy{:});
xlabel ('value of x');
ylabel ('function value');
  1 Comment
Ikenna Okoye
Ikenna Okoye on 28 Nov 2018
I wanted to do something similar, i have an equation for thrust,F_ava, that depends on two variables 'gamma' and 'ex' and has an outputed vector already, could do this to the code?:
for F_ava(1,:)
xy{1,F_ava} = gamma;
xy{2,F_ava} = ex;
xy{3,F_ava} = ((m_dot.*v_e)+ (P_e-P).*(A_t.*ex));
end
plot(xy{:});
xlabel ('Altitude (km)');
ylabel ('Thrust (N)');

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!