Steven Li
Steven Li

Reputation: 19

plot a nonlinear function matlab

I'm very new to Matlab and have problem plotting this nonlinear 2D function graph using Matlab.

a lot of errors generated after the below is run.

fun1 =  20 + 10 + 15;
fun2 =  20 + (x * 0.00125 ) + 15;
fun3 = (x * 0.0025) + 15;

fplot(fun1,[0 8000])
fplot(fun2,[8000 16000])
fplot(fun2,[16000 positive infinity])

I appreciate a lot to your efforts and kindness for replying my question Best Regards

Upvotes: 0

Views: 2102

Answers (3)

mhmsa
mhmsa

Reputation: 475

If you do some reading in fplot you will find out that for fplot(fun,limits) fun must be

  • The name of a function
  • A string with variable x that may be passed to eval, such as 'sin(x)', 'diric(x,10)', or '[sin(x),cos(x)]'
  • A function handle

so in your case you need to change all of you fun to strings just add ' before and after the expression

as for the last line change it to be

fplot(fun2,[16000 inf])

although i don't think this would give you any good results

Upvotes: 0

Daniel
Daniel

Reputation: 36710

Your first three expressions do not define functions. Please read the documentation about the correct syntax.

fun1 = @(x)(20 + 10 + 15);

Upvotes: 1

Chris Taylor
Chris Taylor

Reputation: 47392

First create a file fun.m which contains your function definition

function y = fun(x)
  if x < 8000
    y = 20 + 10 + 15;
  elseif x < 16000
    y = 20 + (x * 0.00125) + 15;
  else
    y = x * 0.0025 + 15;
  end
end

Then you can plot it with

fplot(@fun, [0 24000])

which results in

enter image description here

Upvotes: 0

Related Questions