
#include<stdioh>
int main()
{
int x,y;
scanf("%d",&x);
if(x<-10)
y=0;
else if(x<100) y=5x+1;
else
y = 5x + 1; //这个表达式的值是什么啊
printf("%d\n",y);
return 0;
}
#include<stdioh>
void main()
{
float x,y;
scanf("%f",&x);
if(x>0)
y=xx;
else if(x==0)
y=2x-1;
else
y=-3xx-1 ;//这里少个分号
printf("%2f",y);
}
怎样用matlab来定义分段函数,首先要根据分段函数建立自定义函数,然后在命令窗口调用或其他程序里调用。
现举例说明:
1、建立自定义函数文件,piecewise1m
function y=piecewise1(t)
%分段函数
n=length(t); %计算所输入t的个数
for i=1:n
if t(i)>=2 %如果t>=2时,y=1
y(i)=1;
elseif t(i)>=-2 & t(i)<2 %如果t[-2,2)时,y=t²
y(i)=t(i)^2;
else y(i)=-1; %如果t<-2时,y=-1
end
end
2、在命令窗口中调用
>>t=0:05:10;
>>y=piecewise1(t)
3、运行结果
其他类似的分段函数,可以参考上述来写代码。
#include <iostream>
using std::cout;
using std::endl;
double gety(double x);
int main()
{
double x=3;
double y=gety(x);
cout<<y;
return 0;
}
double gety(double x){
if(x<0 && x>-10){
return x+1;
}else if(x==0){
return x-1;
}else if(x>0 && x<10){
return 2x+1;
}else{
return false;
}
};
#include <stdioh>
#include <mathh>
int main(void)
{
int repeat, ri;
double x, y;
scanf("%d", &repeat);
for(ri = 1; ri <= repeat; ri++){
scanf("%lf",&x);
y=x>=0sqrt(x):pow(x+1,2)+2x+1/x;
printf("f(%2f) = %2f\n", x, y);
}
}
点击至文库查看全文>
原发布者:bitian216
f(x)的定义如下:
1、写一个函数文件f(x)实现该函数,要求参数x可以是向量;2、作出该函数的图形;3、求出f(x)的零点与最值。解:(1)、编写M函数文件functiony=f(x)n=length(x);ifx=0&x<10&x~=2&x~=3y=x^2+5x+6;elsey=x^2-x-1;end(2)、把文件fm放置在搜索路径上(3)、运行指令令x=5,则在命令窗口输入指令y=f(5)得到答案:y=56(2)图形x1=(-5):001:0;y1=x1^2+x1-6;plot(x1,y1,'m-');holdonx2=0:001:10;y2=x2^2-5x2+6;plot(x2,y2,'r:');holdonx3=10:001:15;y3=x3^2-x3-1;plot(x3,y3);x4=-4;y4=x4^2-x4-1;plot(x4,y4,'p');holdonx5=2;y5=x5^2-x5-1;plot(x5,y5,'b');holdonx6=3;y6=x6^2-x6-1;plot(x6,y6,'g');title('函数f(x)的图形');text(-4,-20,'曲线f1(x)=x^2+x-6');text(2,40,'曲线f2(x)=x^2-5x+6');text(10,146,'曲线f3(x)=x^2-x-1');legend('f1(x)','f2(x)','f3(x)','x=-4','x=2','x=3');结果如图:
(2)f(x)的零点①当x>p1=[1,1,-6];>>x1=roots(p1);x1=-32由题意可知,x的取值范围在(-∞,0),所以x1=2舍去,即f(x)的零点之一为x1=-3②当x>=0&x>p2=[1,-5,6];>>x2=roots(p2)x2=
#include <stdioh>
int main(void){
int x;
int y;
scanf("%d",&x);//得到x的值
if(x<1){
y=x;
}else if (x>=1&&x<12) {
y=5x-2;
}else {
y=8x-10;
}
printf("y= %d\n",y);
return 0;
}
以上就是关于C语言编写分段函数全部的内容,包括:C语言编写分段函数、c语言编程分段函数。、MATLAB求分段函数,怎么写代码呀等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)