كيفية رسم دالة مثلثية ووظائف خطوة أخرى في ماتلاب [مغلق]

StackOverflow https://stackoverflow.com//questions/20033809

  •  21-12-2019
  •  | 
  •  

سؤال

كما يشير العنوان، أريد أن أعرف كيفية رسم دالة مثلثية في ماتلاب.على سبيل المثال

f(x) = 1-|x| for |x| < 1 and f(x) = 0 otherwise

وكذلك الوظائف:

Af(x) = A for x >= 0 and Af(x) = 0 for x < 0; -f(x) = -1 for x >= 0 and -f(x) = 0 for x < 0
هل كانت مفيدة؟

المحلول

أفترض أنك لا تستخدم المتغيرات الرمزية.للتخطيط ثنائي الأبعاد في MATLAB، تحتاج إلى متجهين متساويين الحجم، واحد لكل محور، لذلك تحتاج إلى إنشاء متجه المحور السيني ومتجه المحور الصادي.في مثالك f (x) = 1- | x | ل | x | <1 يمكنك القيام بذلك:

x = linspace(-5,5,500); %x-axis vector from -5 to 5 with 500 points
y = zeros(1,500);  %y-axis vector initialized to 0, also 500 points like the x-axis vector
y(abs(x) < 1) = 1- abs(x(abs(x)<1)); %the points corresponding to |x|< 1 are set to |x|

figure() %new figure
plot(x,y) %plot
box off  %removing box
grid on  %adding grid
xlabel('x axis', 'FontSize', 15) %label of x axis
ylabel('y axis', 'FontSize', 15) %label of y axis
axis([x(1), x(end), -0.5, 1.5])  %axis limits

وبذلك تحصل على قطعة أرض مثل هذه:

enter image description here

بالنسبة للوظائف الأخرى، عليك المتابعة كما هو الحال في هذه الوظيفة، قم ببناء متجه المحور السيني ومتجه المحور الصادي.

تحديث: وفي المثال الآخر: f(x) = A for x >= 0 and f(x) = 0 for x < 0:

A = 3;
x = linspace(-5,5,500); %x-axis vector from -5 to 5 with 500 points
y = zeros(1,500);  %y-axis vector initialized to 0, also 500 points like the x-axis vector
y(x >= 0) = A; %the points corresponding to x >= 0 are set to A

figure() %new figure
plot(x,y) %plot
box off  %removing box
grid on  %adding grid
xlabel('x axis', 'FontSize', 15) %label of x axis
ylabel('y axis', 'FontSize', 15) %label of y axis
axis([x(1), x(end), -0.5, 3.5])  %axis limits

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top