MATLAB: Plotting two curves together in one graph for different intervals

MATLABplot

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?

Best Answer

  • 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));