MATLAB Answers

How to plot a single variable function with constraints

48 views (last 30 days)
Mihai Giurca
Mihai Giurca on 20 Mar 2020
Edited: Matt J on 20 Mar 2020
Hello, I am trying to plot f(x) = 4 - x*sin(x) with constraints x>=6, x<=9 and log(x)>= -1. I tried using meshgrid but the graph came out wrong + it was in the 3rd dimension. Can anyone tell me how I should go about plotting a simple function with these constraints? Thank you!

  2 Comments

Ameer Hamza
Ameer Hamza on 20 Mar 2020
What does the constraint log(x)>= -1 mean? Does it mean x >= exp(-1)? How does this combine with x>=6, x<=9?
Mihai Giurca
Mihai Giurca on 20 Mar 2020
I'm honestly not sure, my professor gave me these constraints and I'm not sure what the log is supposed to mean.

Sign in to comment.

Answers (2)

Ameer Hamza
Ameer Hamza on 20 Mar 2020
f = @(x) 4 - x*sin(x);
fplot(f, [6 9]);

  0 Comments

Sign in to comment.


Matt J
Matt J on 20 Mar 2020
Edited: Matt J on 20 Mar 2020
You can assign NaN to anything you don't want plotted. Example,
x=linspace(0,2,1000);
f=sin(x);
f( 0.5<=x & x<=1)=nan;
plot(x,f);

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019b