Huber function matlab plotting with two intervals

16 ビュー (過去 30 日間)
Kentman
Kentman 2015 年 3 月 31 日
編集済み: Walter Roberson 2017 年 11 月 15 日
Hello,
I was trying to write this simple matlab function:
rho(eps)=0.5*eps^2 for eps<=k
rho(eps)=k*eps-0.5*k^2 for eps>k
for k=1.345
so my solution was:
k=1.345;
for eps=0:0.001:6
if eps<=k
f1=0.5*(eps^2);
else
f1=k*eps-0.5*(k^2);
end %if loop
end %for loop
plot(eps,f1)
There was almost empty graph and sometimes only linear function although this function is supposed to be nonlinear in the first part and linear in the other part.
and also i'm supposed to plot the derivative of this function after this is complete.

採用された回答

Torsten
Torsten 2015 年 3 月 31 日
k=1.345;
eps=zeros(6001);
f1=zeros(6001);
for i=1:6001
eps(i)=(i-1)*0.001;
if eps(i)<=k
f1(i)=0.5*(eps(i)^2);
else
f1(i)=k*eps(i)-0.5*(k^2);
end %if loop
end;
plot(eps,f1)
Best wishes
Torsten.

その他の回答 (3 件)

Kentman
Kentman 2015 年 3 月 31 日
Is there a way to plot the derivation of this function directly by matlab or we need to do it by hand then apply the result?

Torsten
Torsten 2015 年 3 月 31 日
Or more "MATLAB-like":
k=1.345;
eps=0:0.001:6;
f1=zeros(size(eps));
epsf=eps<=k;
f1(epsf)=0.5*eps(epsf).^2;
epsf=eps>k;
f1(epsf)=k*eps(epsf)-0.5*k^2;
plot(eps,f1);
Best wishes
Torsten.

Kentman
Kentman 2015 年 3 月 31 日
so the final matlab will be
clear all;close all;clc;
k=1.345; eps=zeros(6001); f1=zeros(6001); f2=zeros(6001); for i=1:6001 eps(i)=(i-1)*0.001; if eps(i)<=k f1(i)=0.5*(eps(i)^2); f2(i)=eps(i); else f1(i)=k*eps(i)-0.5*(k^2); f2(i)=k; end %if loop end; %for loop
plot(eps,f1,'k-') hold on plot(eps,f2,'b-')

カテゴリ

Help Center および File ExchangeR Language についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by