who to write this function in another way?

I did this function but I didn't want like this long. I try to put in MATLAB Function in simulink
I want like this but I did not where my mistake?
outputvalue = map(sensorValue, low input , high input , low output , high input);
y = map(u, -1 , 1 , 0 , 0.5);
that what I did
function y = fcn(u)
if u <(-0.9)
y=0.25;
elseif u < (-0.8)
y=0.20;
elseif u < (-0.6)
y=0.15;
elseif u < (-0.4)
y=0.10;
elseif u < (-0.2)
y=0.05;
elseif u < 0.2
y=0;
elseif u < 0.4
y=0.30;
elseif u < 0.6
y=0.35;
elseif u < 0.8
y=0.40;
elseif u < 0.9
y=0.45;
elseif u < 1.5
y=0.50;
else
y=1;
end

2 件のコメント

KSSV
KSSV 2021 年 9 月 22 日
u < (-0.8)
can be
u < -0.8
Mohammad Alhaddad
Mohammad Alhaddad 2021 年 9 月 22 日
i know but I didnot want the second code I want convert to this
outputvalue = map(sensorValue, low input , high input , low output , high input);
y = map(u, -1 , 1 , 0 , 0.5);

サインインしてコメントする。

回答 (3 件)

Chunru
Chunru 2021 年 9 月 22 日

0 投票

fcn(-5)
ans = 0.2500
fcn(.34)
ans = 0.3000
fcn(4)
ans = 1
function y = fcn(u)
x0 = [-inf -0.9 -0.8 -0.6 -0.4 -0.2 0.2 0.4 0.6 0.8 0.9 1.5];
y0 = [0.25 0.20 0.15 0.10 0.05 0 0.30 0.35 0.40 0.45 0.50 1 ];
idx = find(u > x0, 1, 'last' );
y = y0(idx);
end

3 件のコメント

Mohammad Alhaddad
Mohammad Alhaddad 2021 年 9 月 22 日
no I did not want put the number I want but values between this numbers input -1:1:100
output 0:1:100
Chunru
Chunru 2021 年 9 月 22 日
What do you mean by saying "this numbers input -1:1:100 output 0:1:100" ?
Mohammad Alhaddad
Mohammad Alhaddad 2021 年 9 月 22 日
the input beween -1 to 1 and Divided into 100 sections. and match with out put from 0 to 1 and Divided into 100 sections.

サインインしてコメントする。

Mohammad Alhaddad
Mohammad Alhaddad 2021 年 9 月 22 日
編集済み: Mohammad Alhaddad 2021 年 9 月 22 日

0 投票

Like this but this in ardino sysem

1 件のコメント

Chunru
Chunru 2021 年 9 月 22 日
This looks like a new question and you should have started a new question. The old question has been answered already.
Anyway, the solution to the problem is rather straightforwad:
x = 0:.1:1023;
y = map(x, 0, 1023, 0, 255);
plot(x(1:200), y(1:200));
function y = map(x, in0, in1, out0, out1)
y = floor((x-in0)/(in1-in0) * (out1-out0)) + out0;
end

サインインしてコメントする。

Steven Lord
Steven Lord 2021 年 9 月 22 日

0 投票

Try using the discretize function.

カテゴリ

ヘルプ センター および File ExchangeProgrammatic Model Editing についてさらに検索

質問済み:

2021 年 9 月 22 日

回答済み:

2021 年 9 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by