plotting two curves together in one graph for different intervals

조회 수: 11 (최근 30일)
Hi,
I have two functions that I want to plot for differnt intervals. I wrote a code but the output is kinda wiered. Can someone please help me what I am doing wrong with the code?
My functions are:
f(x1)= 22528*((0.2808-0.1031)*x +(0.1666-0.2808)*(144728064/(22528-x))+0.2808*(72982528/x)+0.1031*(22528)) from [0 to 7552.2278]
f(x2)= 22528*((0.2808-0.1031)*x +(0.1666-0.1031)*(72982528/x)+0.1031*(144728064/(22528-x))+0.1031*(22528)) from [7552.2278 to 22528]
I wrote below code but got strange output,
y_1 = @(x) 22528.*((0.2808-0.1031).*x +(0.1666-0.2808).*(144728064 ./(22528-x))+0.2808.*(72982528./x)+0.1031.*(22528))
y_2 = @(x) 22528.*((0.2808-0.1031).*x + (0.1666-0.1031).*(72982528./x)+0.1031.*(144728064./(22528-x))+0.1031.*(22528))
x_1 = 0:0.1:7552.2278; // y_1 Interval
x_2 = 7552.2278:0.1:22528; //y_2 Interval
plot(x_1,y_1(x_1),'b',x_2,y_2(x_2),'b')
Please help me what I am doing wrong? Also I want to get all minimum points how I write the code for that?
  댓글 수: 1
Tania Malik
Tania Malik 2020년 5월 12일
I think the problem was my increment value. I changed it to 100 and got result. Now my question is how to get the minimum of this single curve?

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

채택된 답변

Johannes Hougaard
Johannes Hougaard 2020년 5월 12일
Hi Tania
When plotting a function you've already described as a function handle I'd use fplot rather than plot.
As far as I can see this will do the trick
y_1 = @(x) 22528.*((0.2808-0.1031).*x +(0.1666-0.2808).*(144728064 ./(22528-x))+0.2808.*(72982528./x)+0.1031.*(22528));
y_2 = @(x) 22528.*((0.2808-0.1031).*x + (0.1666-0.1031).*(72982528./x)+0.1031.*(144728064./(22528-x))+0.1031.*(22528));
x_1 = [0 7552.2278]; % y_1 Interval
x_2 = [7552.2278 22528]; % y_2 Interval
figure;
fplot(y_1,x_1);
hold on
fplot(y_2,x_2);
Regarding the minimum I can't seem to find a minimum for y_1 but for y_2 the minimum is found by using fminsearch
x_min = fminsearch(y_2,min(x_2));
  댓글 수: 2
Tania Malik
Tania Malik 2020년 5월 12일
Thank you! I got the nice graph. Is there any way I can get the minimum point considering it as a single curve?
Johannes Hougaard
Johannes Hougaard 2020년 5월 12일
To do that I'd create a separate function (see attached myfun) and use the fminsearch option
x_min = fminsearch(@myfun,mean([x_1,x_2]))
But you've pretty much found it yourself in the intersect between your x-ranges.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by