Creating a function
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Question 1 Create a function called f that satisfies the following criteria: For values of x>2, f(x)=x^2 For values of x<=2, f(x)=2x Plot your results for values of x from -3 to 5. Choose your spacing to create a smooth curve.
Question 2 Create a function called g that satisfies the following criteria: For x<-pi, g(x)=-1 For x>=-pi and x<=pi, g(x)=cos(x) For x>pi, g(x)= -1 Plot your results for values of x from -2pi to 2pi. Choose your spacing to create a smooth curve.
2 Commenti
Risposte (2)
Matt Fig
il 3 Apr 2011
1. Do you think that copying and pasting a few obvious homework problems will get much of a positive response? From your post it is not obvious whether you are a spam robot or a living human being, except that spam robots probably would be programmed to imitate human interaction better than that.
2. Most participants here expect a minimal of effort on the part of the asker. So show what you have tried, where you are stuck, and ask a specific MATLAB question. It is very likely that you will get good help this way.
EDIT
O.k., now we can get somewhere. IF statements do not pick out array elements like you are thinking they do. I would use logical indexing for question 1. Here is an example for you to play with. It doesn't solve your problem, but will teach you some things to solve it. Copy and paste these lines, then read and think about the output.
x = 0:.5:3
y = zeros(1,length(x))
idx1 = x>1
idx2 = x<=1
x(idx1)
y(idx1)
x(idx2)
y(idx2)
x(idx2).^2
For the rest, look in the documentation for the keyword function.
2 Commenti
Jan
il 3 Apr 2011
"Create a function" means, that you should create a function. Look in the documentation for a description: "doc function". You define the variable x *after* using it - it must be available *before*.
Walter Roberson
il 3 Apr 2011
f := @(x) (-2*x*sin((x>2)-1)+x^2*sin(x>2))/sin(1);
By the time you figure out why that works, you should be well prepared to handle the second part.
I would, by the way, not recommend submitting this answer for your assignment: you are going to have to document why it works and how you created such an odd expression.
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!