how to plot y=|x| function without using abs,

hi im trying to plot two functions the first y=|x| the second y=|x+4|-|x^2-7| without abs func but with for and if please help thanks

댓글 수: 1

James Tursa
James Tursa 2016년 12월 3일
What have you done so far? What specific problems are you having with your code?

댓글을 달려면 로그인하십시오.

답변 (1개)

bio lim
bio lim 2016년 12월 3일

0 개 추천

Well you have to find the regions where your values of y are positive and negative. Here is an example of y = |x|.
x = -100:100;
for i = 1:length(x)
if x(i)<0
y(i) = -x(i);
else y(i) = x(i);
end
end

댓글 수: 4

thanks now i need to plot y=|x+4|-|x^2-7| in -10:0.01:10 region without abs
bio lim
bio lim 2016년 12월 3일
Well, as James Tursa wrote, you have to show us what you have done so far.
clear all;close all;clc
x = -10:0.01:10;
for i = 1:length(x)
if x(i)+4>0
y(i)=-x(i)+4;
else y(i) = -x(i)-4;
end
for j=1:length(x)
if x(j).^2+7>0
g(j)=x(i).^2+7;
else g(j)=-x(i).^2-7;
end
end
plot(x,y)
hold on
plot(x,g)
x = -10:0.01:10;
% let's call a = |x+4|
% b = |x^2 - 7|
for i = 1:length(x)
if (x(i)+4<0)
a(i) = -(x(i)+4);
else a(i) = x(i)+4;
end
end
for i = 1:length(x)
if (x(i)^2-7<0)
b(i) = -(x(i)^2 -7);
else b(i) = x(i)^2 -7;
end
end
y = a - b;

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Labels and Styling에 대해 자세히 알아보기

질문:

2016년 12월 3일

댓글:

2016년 12월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by