필터 지우기
필터 지우기

Use a for loop to evaluate

조회 수: 5 (최근 30일)
Robert
Robert 2013년 2월 9일
use a for loop to evaluate the function y = {exp(x+1) if x<-1, 2+cos(pi*x) if -1<=x<5, 10*(x-5)+1 if x>=5}
e^(x+1) if x<-1
y = {2+cos(pi*x) if -1<=x<5
10(x-5)+1 if x>=5
I am having trouble correctly setting up the for loop to store the values because the final product of this, is to plot the function y(x).
  댓글 수: 2
Robert
Robert 2013년 2월 9일
I forgot the range for the loop which is -2 to 6 increasing by 1 each time. Sorry!
Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 9일
What have you done so far?

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

답변 (2개)

Ahmed
Ahmed 2013년 2월 9일
according to my little knowledge if you need to store this value for every loop in a Matrix where every inner for loop represent a row you can do this
if
y=zeros(9)
x=input('x=')
for i1=-2:6
y(i1)=.....
end
then y will be an array whose values are stored in the above Zero Matrix
Does this help ?
  댓글 수: 1
Image Analyst
Image Analyst 2013년 2월 9일
편집: Image Analyst 2013년 2월 9일
You cannot have zero or negative indexes, like -2 or -1 or 0. You can either add 3 to i1, or use a counter like I did in my Answer.

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


Image Analyst
Image Analyst 2013년 2월 9일
편집: Image Analyst 2013년 2월 9일
Try something like this:
index = 1;
for x = -2 : 1 : 6
if x < -1
% Insert code/formula here
y(index) = .........whatever.....
else if x >= -1 && x < 5
% Insert other code/formula here
else
% x >= 5
% Insert code/formula here
end
index = index + 1;
end
% Plot it.
plot(x,y, 'bo-', 'LineWidth', 3, 'MarkerSize', 15);
title('Plot of y vs. x', 'FontSize', 25);
  댓글 수: 2
Robert
Robert 2013년 2월 10일
Thank you. I will ask if you could generically go where the y(index) first appears. Basically what would the "whatever" be? I'm still very new to Matlab
Image Analyst
Image Analyst 2013년 2월 10일
You insert the appropriate formula from your question:
y = e^(x+1) if x<-1
y = {2+cos(pi*x) if -1<=x<5
y = 10(x-5)+1 if x>=5

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by